PEP 245

Paul Robinson paul.robinson at businesscollaborator.com
Tue Apr 3 08:51:49 EDT 2001


Alex Martelli wrote:
> 
> "Alex Shindich" <shindich at itginc.com> wrote in message
> news:mailman.986255463.27488.python-list at python.org...
> > >in C++, one normally uses _private_ inheritance for this specific purpose
> > There is nothing private in Python. There is "__" name mangling of course,
> > but that is it.
> 
> Exactly.  In particular, '__' name mangling does not apply to inheritance.

What exactly did you mean by that?

The example below (provided by a colleague, thanks Steve) would seem to
show that name mangling does apply to inheritance...

    >>>class Base:
    ...    def __spam(self):     # replaced with _Base__spam
    ...        print 'Hello, world!'
    ...    def test(self):
    ...        self.__spam()     # replaced with _Base__spam, which is
the correct name
    ...
    >>>class Subclass(Base):
    ...    def test(self):
    ...        self.__spam()     # replaced with _Subclass__spam, which
is the WRONG name
    ...
    >>>base_thing = Base()
    >>>subclass_thing = Subclass()
    >>>base_thing.test()
    Hello, world!
    >>>subclass_thing.test()
    Traceback (innermost last):
      File "", line 1, in ?
      File "", line 3, in test
    AttributeError: _Subclass__spam

Paul Robinson
Business Collaborator Development Manager
Enviros Software Solutions
WWW: http://www.businesscollaborator.com




More information about the Python-list mailing list