title: Does Python require intimate knowledge of all classes in the inheritance chain?
body: <p>Python classes have no concept of public/private, so we are told to not touch something that starts with an underscore unless we created it.  But does this not require complete knowledge of all classes from which we inherit, directly or indirectly?  Witness:</p>
  
  <pre><code>class Base(object):
      def __init__(self):
          super(Base, self).__init__()
          self._foo = 0
  
      def foo(self):
          return self._foo + 1
  
  class Sub(Base):
      def __init__(self):
          super(Sub, self).__init__()
          self._foo = None
  
  Sub().foo()
  </code></pre>
  
  <p>Expectedly, a <code>TypeError</code> is raised when <code>None + 1</code> is evaluated.  So I have to know that <code>_foo</code> exists in the base class.  To get around this, <code>__foo</code> can be used instead, which solves the problem by mangling the name.  This seems to be, if not elegant, an acceptable solution.  However, what happens if <code>Base</code> inherits from a class (in a separate package) called <code>Sub</code>?  Now <code>__foo</code> in my <code>Sub</code> overrides <code>__foo</code> in the grandparent <code>Sub</code>.</p>
  
  <p>This implies that I have to know the entire inheritance chain, including all "private" objects each uses.  The fact that Python is dynamically-typed makes this even harder, since there are no declarations to search for.  The worst part, however, is probably the fact <code>Base</code> might inherit from <code>object</code> right now, but in some future release, it switches to inheriting from <code>Sub</code>.  Clearly if I know <code>Sub</code> is inherited from, I can rename my class, however annoying that is.  But I can't see into the future.</p>
  
  <p>Is this not a case where a true private data type would prevent a problem?  How, in Python, can I be sure that I'm not accidentally stepping on somebody's toes if those toes might spring into existence at some point in the future?</p>
  
  <p><em>EDIT</em>: I've apparently not made clear the primary question.  I'm familiar with name mangling and the difference between a single and a double underscore.  The question is: how do I deal with the fact that I might clash with classes <em>whose existence I don't know of right now</em>?  If my parent class (which is in a package I did not write) happens to start inheriting from a class with the same name as my class, even name mangling won't help.  Am I wrong in seeing this as a (corner) case that true private members would solve, but that Python has trouble with?</p>
  
  <p><em>EDIT</em>: As requested, the following is a full example:</p>
  
  <p>File <code>parent.py</code>:</p>
  
  <pre><code>class Sub(object):
      def __init__(self):
          self.__foo = 12
      def foo(self):
          return self.__foo + 1
  class Base(Sub):
      pass
  </code></pre>
  
  <p>File <code>sub.py</code>:</p>
  
  <pre><code>import parent
  class Sub(parent.Base):
      def __init__(self):
          super(Sub, self).__init__()
          self.__foo = None
  Sub().foo()
  </code></pre>
  
  <p>The grandparent's <code>foo</code> is called, but my <code>__foo</code> is used.</p>
  
  <p>Obviously you wouldn't write code like this yourself, but <code>parent</code> could easily be provided by a third party, the details of which could change at any time.</p>