Will python never intend to support private, protected and public?

Paul Rubin http
Fri Sep 30 01:51:54 CEST 2005

"Fredrik Lundh" <fredrik at pythonware.com> writes:
> from yourcode import Secret
> class Secret(Secret):
>     def gethidden(self):
>         return self.__hidden

Heh, interesting, and it occurs to me that you could do that by
accident (A inherits from B, and then something imports B and makes an
inheriting class that inadvertently happens to be called A).  I.e. the
name mangling can fail its intended purpose.

# your code:

class Secret:
    def __init__(self):
        self.__hidden = "very secret value"

class Parrot(secret): pass

# my code

from yourcode import Parrot

class Secret(Parrot):  # I'm not aware that you also have a Secret class
   def spam(self, x):
      self.__hidden = zoo(x)   # clobbers yourcode's version of secret

   def eggs(self):             # might inadvertently read your version's
         return self.__hidden
      except AttributeError:

More information about the Python-list mailing list