Why less emphasis on private data?

Paul Rubin http
Mon Jan 8 04:21:04 CET 2007

"Paul Boddie" <paul at boddie.org.uk> writes:
> > Consider that the above three class definitions might be in separate
> > files and you see how clumsy this gets.
> What are you trying to show with the above? The principal benefit of
> using private attributes set on either the class or the instance is to
> preserve access, via self, to those attributes defined in association
> with (or within) a particular class in the inheritance hierarchy, as
> opposed to providing access to the "most overriding" definition of an
> attribute. This is demonstrated more effectively with a method on class A:

Right, the problem is if those methods start changing the "private"
variable.  I should have been more explicit about that.

class A:
   def __init__(self):
      self.__x = 3
   def foo(self):
      return self.__x

class B(A): pass

class A(B):
   def bar(self):
     self.__x = 5   # clobbers private variable of earlier class named A

More information about the Python-list mailing list