derived / base class name conflicts
christopherlmarshall at yahoo.com
christopherlmarshall at yahoo.com
Thu Nov 10 21:50:57 EST 2005
Fredrik Lundh wrote:
> christopherlmarshall at yahoo.com wrote:
>
> > Now, 'i' might have already been defined by A or by the call to
> > A.__init__() so if you define it without knowing that, you could be
> > changing the behavior of A's methods in unknown ways, which is
> > obviously a bad thing.
>
> http://docs.python.org/tut/node11.html#SECTION0011600000000000000000
>
> </F>
I see. Thanks for the link.
So putting two underscores in front of an instance variable (or any
identifier used inside the scope of a class statement) invokes a name
mangling mechanism ( __x becomes __classname_x internally)
so the following would not result in any conflicts
class A:
def __init__(self):
self.__i= 0
class B(A):
def __init__(self):
A.__init__(self)
self.__i= 1
Is it commonplace to use underscores when defining derived class
instance variables, or is this considered against the grain of python?
Chris Marshall
More information about the Python-list
mailing list