[Tutor] Calling private base class methods

Jorgen Bodde jorgen.maillist at gmail.com
Thu Apr 12 10:46:53 CEST 2007


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;

>>> p = D()
>>> p.someMethod()
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "<interactive input>", line 3, in someMethod
AttributeError: 'super' object has no attribute '_D__baseMethod'

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.

When I do;

>>> class E(object):
... 	def someMethod(self):
... 		print 'Hello'
... 		
>>> class F(E):
... 	def otherMethod(self):
... 		super(F, self).someMethod()
... 		print 'There'
... 		
>>> p = F()
>>> p.otherMethod()
Hello
There
>>>

This seems to work.

Thanks in advance,
- Jorgen


More information about the Tutor mailing list