inheritance problem
Mike C. Fletcher
mcfletch at rogers.com
Tue Oct 1 12:26:19 EDT 2002
Python 2.2 has a function called super...
super(type) -> unbound super object
super(type, obj) -> bound super object; requires isinstance(obj, type)
super(type, type2) -> bound super object; requires issubclass(type2, type)
Which lets you call the super-class (base-class) method like so:
super( InheritedClass, self).methodname( arg1, arg2 )
Before Python 2.2, this was done by explicitly naming the super-class:
BaseClass.methodname( self, arg1, arg2 )
As a note, though, in BaseClass methods called for an InheritedClass,
the method-lookup is via run-time _name_ lookup, so you get whatever the
object (the InheritedClass instance) says it's various methods are.
As an example:
class A( object ):
def a( self ):
return self.b()
def b( self ):
return 'A'
class B( A ):
def b( self ):
return 'B'
class C( A ):
def b( self ):
return super(C,self).b()+'C'
print A().a()
print B().a()
print C().a()
Will print:
A
B
AC
HTH,
Mike
gabor wrote:
...
> then i created an inherited class (let's call it InheritedClass):
> and override displayData
>
> but there's the problem:
> in that windowIDdictionary, the type of the self's is BaseClass,
> so it won't call InheritedClass's methods :-((((
>
> any ideas how to do this correctly?
> as i know there is nothing like casting in python :-((
>
> i have some solutions like the inerited class's constructor will have to
> manually call a method in the baseclass.... but i wanted to make it a
> way, so the inherited class doesn't have to deal with the baseclass,
> except for the constructor...
>
> thanks,
> gabor
>
_______________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/
More information about the Python-list
mailing list