Name mangling
Fredrik Lundh
effbot at telia.com
Tue May 9 12:07:43 EDT 2000
ullrich at math.okstate.edu wrote:
> > section 9.6 in the tutorial discusses this:
> >
> > http://www.python.org/doc/current/tut/node11.html
> >
> > "Any identifier of the form __spam (at least two
> > leading underscores, at most one trailing under-
> > score) is now textually replaced with
> > _classname__spam, where classname is the
> > current class name with leading underscore(s)
> > stripped. This mangling is done without regard
> > of the syntactic position of the identifier, so it
> > can be used to define class-private instance
> > and class variables, methods, as well as globals,
> > and even to store instance variables private to
> > this class on instances of other classes"
>
> Thanks. But actually I'd found that part in the docs,
> but I don't see how it implies that the silly snippet I
> posted should not work. Probably I'm just being dense,
> but usually when I find the right page in the docs I get
> an "aha" out of it - here 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>
More information about the Python-list
mailing list