newbie-name mangling?

bobueland at yahoo.com bobueland at yahoo.com
Tue Dec 13 22:05:32 CET 2005


I'm reading van Rossum's tutorial. Mostly it is well written and
examples are given. However sometimes I get lost in a text, when it
doesn't give any examples and no clues. There are several examples of
this in chapter 9 about classes. Here's one from 9.6 (Private
Variables). I quote

"There is limited support for class-private identifiers. Any identifier
of the form __spam (at least two leading underscores, at most one
trailing underscore) is textually replaced with _classname__spam, where
classname is the current class name with leading underscore(s)
stripped.
This mangling is done without regard to the syntactic position of the
identifier, so it can be used to define class-private instance and
class variables, methods, variables stored in globals, and even
variables stored in instances. private to this class on instances of
other classes. ...
Outside classes, or when the class name consists of only underscores,
no mangling occurs. Name mangling is intended to give classes an easy
way to define "private" instance variables and methods, without
having to worry about instance variables defined by derived classes, or
mucking with instance variables by code outside the class. Note that
the mangling rules are designed mostly to avoid accidents;"

Could someone provide an example of the above or direct me to a page
where it is used.

Bob




More information about the Python-list mailing list