No subject


Sun Nov 12 08:01:13 CET 2006


of an attribute begins with a double-underscore, it's just as if it had 
been declared private:

class C:
  def __init__(self):
    self.__a = 12
  def get_a(self):
    return self.__a
o = C()
o.__a = 13       # Does not work
print o.__a      # Again, does not work
print o.get_a()  # Works

class CC(C):
  def __init__(self):
    C.__init__(self)
    print self.__a    # Does *not* work

The attribute __a is private: it can be seen only in the class C. It's 
impossible to read or set it from outside the class, even in one of its 
sub-classes. This sounds like a big restriction, but is actually very often 
used in OO programming.

The actual mechanism involves a "name-mangling", and that's what the 
comment tried to explain. But it's not that important: the final result is 
just that any attribute beginning with a double-underscore is private.

HTH
-- 
- Eric Brunel <eric.brunel at pragmadev.com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com



More information about the Python-list mailing list