"private" variables a.k.a. name mangling (WAS: What is print? A function?)

Steven Bethard steven.bethard at gmail.com
Tue Jan 25 14:10:45 EST 2005


Toby Dickenson wrote:
> I have a counterexample. Consider refactoring a class from....
> 
> class B(A):
>     etc
> 
> ....into....
> 
> class C(A):
>     etc
> class B(C):
>     etc
> 
> Usage of some double-undescore attributes moved from B to the new intermediate 
> base class C. Unit tests on B still passed, so that change is safe. right?
> 
> The problem occured because the double-underscore mangling uses the class 
> name, but ignores module names. A related project already had a class named C 
> derived from B  (same name - different module). My refactoring caused 
> aliasing of some originally distinct double-underscore attributes.

Very interesting.  I hadn't ever really thought about it, but I guess 
this shows that even __-mangling won't solve all of the 
attribute-renaming problems...

A somewhat related problem is briefly discussed in Guido's autosuper 
example:
     http://www.python.org/2.2.3/descrintro.html#metaclass_examples
where a base class derived from a class using autosuper but with the 
same name as the superclass might get the wrong self.__super.

Steve



More information about the Python-list mailing list