Calling private base methods

Enrico 4564 at 755189.45
Thu Apr 12 05:12:00 EDT 2007


"Jorgen Bodde" <jorgen.maillist at gmail.com> ha scritto nel messaggio
news:mailman.6393.1176367681.32031.python-list at python.org...
> Hi All,
>
> Now that I am really diving into Python, I encounter a lot of things
> that us newbies find difficult to get right. I thought I understood
> how super() worked, but with 'private' members it does not seem to
> work. For example;
>
> >>> class A(object):
> ...     def __baseMethod(self):
> ...             print 'Test'
>
> Deriving from A, and doing;
>
> >>> class D(A):
> ...     def someMethod(self):
> ...             super(A, self).__baseMethod()
> ...             print 'test3'
>
> Will not work;

if you type
>> dir(A)

you'll get a method named
_A__baseMethod

>From the documentation:
Private name mangling: When an identifier that textually occurs in a class
definition begins with two or more underscore characters and does not end in
two or more underscores, it is considered a private name of that class.
Private names are transformed to a longer form before code is generated for
them. The transformation inserts the class name in front of the name, with
leading underscores removed, and a single underscore inserted in front of
the class name. For example, the identifier __spam occurring in a class
named Ham will be transformed to _Ham__spam. This transformation is
independent of the syntactical context in which the identifier is used. If
the transformed name is extremely long (longer than 255 characters),
implementation defined truncation may happen. If the class name consists
only of underscores, no transformation is done.

Enrico





More information about the Python-list mailing list