Two attributes! Why?

Terry Reedy tjreedy at udel.edu
Mon Dec 1 15:02:48 EST 2008


Emanuele D'Arrigo wrote:
> On Nov 29, 5:21 am, Steven D'Aprano <st... at REMOVE-THIS-
> cybersource.com.au> wrote:
>> This is a side-effect of name-mangling. Double-underscore names are only
>> mangled when they are referred to directly as attributes, not when they
>> are passed to setattr, getattr etc. Those functions don't do any name
>> mangling.

Even as attributes, they are only mangled within the class definition.
"__*
Class-private names. Names in this category, when used within the 
context of a class definition, are re-written to use a mangled form to 
help avoid name clashes between “private” attributes of base and derived 
classes. See section Identifiers (Names). "

Class-private is intended to mean 'private', not known and not used 
outside the class definition.  I believe the 'mangled' form is 
intentionally not documented.

> Ah! That make sense! Thank you!
> 
> It's a bit of an obscure thing right now. I don't see mention of it in
> the description of the setattr/getattr built-ins:
> http://docs.python.org/library/functions.html
> Wouldn't be good to have a hint of the issue there?

I think not.  The functions tell the truth when they say 'no such 
attribute' Any mention might suggest that trying to access private names 
from outside the class is a sensible thing to do.

Perhaps the __* doc quoted above could be improved.  Is anything there 
unclear?

Terry Jan Reedy




More information about the Python-list mailing list