[Tutor] Re: Python Class functionality idiosyncrasy..

dhanvik at gmx.net dhanvik at gmx.net
Wed Oct 22 07:06:18 EDT 2003


Dear Andrei,
thats right .. Dynamic Checking in Python is the reason that 
the base class is allowing the using of function that is 
yet to be defined any there else in the program. But the 
function can be accessed using the 'self' variable..

Any program that would be implemented this way would be bad 
programming and more than that bad designing of OO 
structure for the prgram. 

this was something that struck immediately as being wrong 
and coming form C++ background to Python, I found it 
blasphemous ( :)) )..after all the base class is something 
thats supposed to be the mother of all later classes and 
hence how can it call the functions of the derived class 
which hasnt even been defined... but some thgt and it falls 
in line with the basic line of thgt of python 
implementation..

- Dhanvi

On Wednesday 22 October 2003 04:17 pm, Andrei wrote:
> 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.




More information about the Tutor mailing list