[Tutor] Re: Python Class functionality idiosyncrasy..

Andrei project5 at redrival.net
Wed Oct 22 06:47:54 EDT 2003


dhanvik at gmx.net wrote:

> Dear Friends,
> 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

No, it doesn't call it because no calling is performed at compile time. 
Note that Python has no compile-time checking of whether a method is 
defined or not. After all, what's not there at compile-time, can be 
there at runtime (dynamic as opposed to static languages).

> some tennants of OO techniques ?? How can the base class 
> call functions of the derived class ?? 

It doesn't. I think you've done something wrong, here's my run:

 >>> class base1:
...     def baseFunc1(self):
...         print "Base class Function"
...         self.derivedFunc()
...
 >>> class derived(base1):
...     def derivedFunc(self):
...         print "derived Func..."
...
 >>> b = base1()
 >>> b.baseFunc1()
Base class Function
Traceback (most recent call last):
   File "<input>", line 1, in ?
   File "<input>", line 4, in baseFunc1
AttributeError: base1 instance has no attribute 'derivedFunc'
>>> d = derived()
>>> d.baseFunc1()
Base class Function
derived Func...

So I get an AttributeError, mentioning there's no derivedFunc. Seems 
about right to me (not sure why you got a typeerror). If we look at the 
dicts of b and de:

 >>> dir(b)
['__doc__', '__module__', 'baseFunc1']
 >>> dir(d)
['__doc__', '__module__', 'baseFunc1', 'derivedFunc']

See, d has both methods. So when you call baseFunc1 on it, derivedFunc 
is there for baseFunc1 to access. The method is there at runtime and 
that's all baseFunc1 cares about.


-- 
Yours,

Andrei

=====
Mail address in header catches spam. Real contact info (decode with rot13):
cebwrpg5 at bcrenznvy.pbz. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V 
ernq gur yvfg, fb gurer'f ab arrq gb PP.





More information about the Tutor mailing list