Confusion About Classes

Steve Holden steve at holdenweb.com
Thu Dec 30 08:27:10 EST 2004


Steven Bethard wrote:

> M.E.Farmer wrote:
> 
>> there are no variables in python
[...]
> As M.E.Farmer mentioned, you can't remove underscores on special method 
> names like __init__.  However, when you're declaring an instance 
> variable, e.g.:
> 
>     self.__xyz = True
> 
> then you're choosing the name here, so you can name it whatever you 
> want.  The only reason to use leading double-underscores is if you want 
> Python to name-mangle the variable so it's not (easily) accessible from 
> subclasses.  In most cases, this is unnecessary.
> 
While it's unnecessary in most cases, I'd like to point out that the 
mechanism is mostly to avoid inadvertent clashes in the object namespace.

So, if you are designing a class explicitly to be subclassed, that's 
when the use of mangled names can pay off.

Of course, if the subclass implementor actually *does* need access to 
your instance variables they will curse you if you've mangled them.

unwrapping-that-mangling-isn't-pretti-ly y'rs  - steve
-- 
Steve Holden               http://www.holdenweb.com/
Python Web Programming  http://pydish.holdenweb.com/
Holden Web LLC      +1 703 861 4237  +1 800 494 3119



More information about the Python-list mailing list