Name mangling

ullrich at ullrich at
Tue May 9 18:39:46 CEST 2000

In article <jJWR4.3404$wYl.205175296 at>,
  "Fredrik Lundh" <effbot at> wrote:
> ullrich at wrote:
> > [...] I don't get it at all...
> oh, I forgot to include the important part: the paragraph
> I quoted ends with this sentence:
>     "Outside classes, or when the class name consists
>     of only underscores, no mangling occurs."
> in other words, this works:
> _C__foo = 42
> class C:
>   def __init__(self):
>     self.value = __foo
> c = C()
> </F>

      That was quick - I just came back to say oh I think
I got it and you already replied.

      What I was missing (I think) was the significance
of the words "textually replaced". In fact(??) the text
_defining_ C is changed to

class C:
  def __init__(self):
    self.value = _C__foo

before anything else happens(???) This is not what I
thought it all meant, although it has the same effect
as what I thought happened in case the identifier is an
attribute of the class - it also makes it clear why the
original snippet gives that error and the modified one
doesn't, so this must be it. Aha, thanks.

     I can easily imagine why having

   self.__value = 42

changed automatically to

   self._C__value = 42

could be useful, but I don't see the point to changing

   self.value = __foo


   self.value = _C__foo

this way (this is as much a pathetic excuse
for not realizing what was going on as a
request for an explanstion, although if you
have an example handy that would be fine.)



Sent via
Before you buy.

More information about the Python-list mailing list