[Tutor] Calling private base class methods

Alan Gauld alan.gauld at btinternet.com
Thu Apr 12 12:35:40 CEST 2007

"Jorgen Bodde" <jorgen.maillist at gmail.com> wrote

>>>> 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;

> Is it possible to call a private base method? I come from a C++
> background, and I liked this construction as my base class has 
> helper
> methods so that I do not have to  duplicate code.

But if you declared a method private in C++ a sub class could
not call it either, it is only if it is protected that subclasses can
call base methods. But Python has no protected mode, only
the single underscore naming convention.

So either you need to rename your method with a single underscore
or don't make it private at all. Do you really, really need it to be 
There are very few real cases where private methods are required and
the norm in Python is for public methods, it makes for much more
flexible classes.

Personally I don't like the super() implementation in Python and
tend to use the the more explicit style of call to the superclass.
As in:


But I don't think that would make any difference here.

Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list