[Tutor] Python Class functionality idiosyncrasy..

dhanvik at gmx.net dhanvik at gmx.net
Wed Oct 22 06:20:58 EDT 2003


Dear Friends,
  While just playing arnd with Python .. I just came across 
this curious implementation of Python Object Oriented 
Techniques.. 

The base class here is calling a function of the derived 
class even befre the function of the derived class is 
defined..A similar implementation in C++ would not even 
compile.. My question is doesnt this implementation break 
some tennants of OO techniques ?? How can the base class 
call functions of the derived class ?? 

>>> class base1 :
...     def baseFunc1( self ):
...             print "Base class Function"
...             self.derivedFunc()
...
>>> class derived( base1 ):
...     def derivedFunc(self ):
...             print "derived Func.."
...
>>> b = base1()
>>> b.baseFunc1()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: unbound method baseFunc1() must be called with 
base1 instance as first argument (got nothing instead)

>>> d  = derived()
>>> d.baseFunc1()
Base class Function
derived Func..
>>>


Cheers
Dhanvi K



More information about the Tutor mailing list