Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #32579

Re: Private methods

References (1 earlier) <mailman.1994.1349790529.27098.python-list@python.org> <1krpdak.u0qy9e1a4knspN%real-not-anti-spam-address@apple-juice.co.uk> <50742FD4.8030007@gmail.com> <mailman.2012.1349802524.27098.python-list@python.org> <5074b86b$0$6574$c3e8da3$5496439d@news.astraweb.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2012-11-01 16:45 -0600
Subject Re: Private methods
Newsgroups comp.lang.python
Message-ID <mailman.3171.1351809960.27098.python-list@python.org> (permalink)

Show all headers | View raw


On Tue, Oct 9, 2012 at 5:51 PM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> On Tue, 09 Oct 2012 11:08:13 -0600, Ian Kelly wrote:
>
>> I tend to view name mangling as being more for avoiding internal
>> attribute collisions in complex inheritance structures than for
>> designating names as private.
>
> Really? I tend to view name mangling as a waste of time, and complex
> inheritance structures as something to avoid.

Name mangling is also useful for object tagging.  Suppose you have
object A that is passed object B and needs to track some data
concerning object B, but does not need a strong reference to B.  One
solution is to use a weak-key dictionary, but this relies upon B being
hashable, and I find it neater to just store the data on B itself.
The problem is that whatever name you use might conflict with an
existing attribute belonging to B.


class Tagger(object):

    def tag(self, taggee):
        taggee.__tag = some_tag_data


One of the curious ramifications of Python's name-mangling system is
that even though the attribute above is being set on some arbitrary
object, the mangling that is applied is in any case that of the class
Tagger.  Thus the mangled attribute name ends up being "_Tagger__tag",
which is unlikely to cause a conflict.

There are some disadvantages to tagging.  One is that you can't tag
objects of built-in types.  Another is that you can't tag instances of
classes with __slots__.  I tend to view the latter as another reason
to avoid using __slots__.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Private methods real-not-anti-spam-address@apple-juice.co.uk (D.M. Procida) - 2012-10-09 14:24 +0100
  Re: Private methods Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-10-09 14:51 +0100
    Re: Private methods real-not-anti-spam-address@apple-juice.co.uk (D.M. Procida) - 2012-10-09 14:59 +0100
      Re: Private methods Demian Brecht <demianbrecht@gmail.com> - 2012-10-09 07:08 -0700
      Re: Private methods Robert Kern <robert.kern@gmail.com> - 2012-10-09 15:12 +0100
      Re: Private methods Tim Chase <python.list@tim.thechases.com> - 2012-10-09 09:21 -0500
      Re: Private methods Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-09 11:08 -0600
        Re: Private methods Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-09 23:51 +0000
          Re: Private methods Demian Brecht <demianbrecht@gmail.com> - 2012-10-09 17:26 -0700
            Re: Private methods real-not-anti-spam-address@apple-juice.co.uk (D.M. Procida) - 2012-10-10 08:03 +0100
              Re: Private methods alex23 <wuwei23@gmail.com> - 2012-10-10 18:34 -0700
                Re: Private methods Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-11 02:08 +0000
                Re: Private methods Dieter Maurer <dieter@handshake.de> - 2012-10-11 08:04 +0200
          Re: Private methods Robert Kern <robert.kern@gmail.com> - 2012-10-10 13:47 +0100
          Re: Private methods Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-01 16:45 -0600
      Re: Private methods Roy Smith <roy@panix.com> - 2012-10-10 09:08 -0400
        Re: Private methods Ramchandra Apte <maniandram01@gmail.com> - 2012-10-10 08:56 -0700
          Re: Private methods Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-10-10 17:13 +0100
            Re: Private methods Grant Edwards <invalid@invalid.invalid> - 2012-10-10 16:30 +0000
            Re: Private methods alex23 <wuwei23@gmail.com> - 2012-10-10 18:39 -0700
  Re: Private methods 88888 Dihedral <dihedral88888@googlemail.com> - 2012-10-10 20:11 -0700

csiph-web