super - is (should) it (be) a reserved word?
Michal Wallace
sabren at manifestation.com
Mon Oct 9 12:01:33 EDT 2000
On Mon, 9 Oct 2000, Grant Edwards wrote:
> >Why do you need that? Calling a method of a class
> >climbs the hierarchy for you, all the way up to where
> >the method was first defined or last overridden.
>
> The example I most often run into is in an __init__ method
> where I want to initialize the features my subclass is adding
> and then pass control and the remaining arguments on up to the
> __init__ method one layer up. The "super" that I want access
> to is the parent of the class containing the reference to
> "super", not the parent of the class of "self".
I see. I was thinking that .super would be a class member,
not an instance member. If you define it like so:
class Dessert:
def __init__(self):
print "dessert!"
class Pie(Dessert):
super = Dessert
def __init__(self):
print "pie is a..."
Pie.super.__init__(self)
class BananaCremePie(Pie):
super = Pie
def __init__(self):
print "banana creme pie is a..."
BananaCremePie.super.__init__(self)
then:
>>> weapon = BananaCremePie()
banana creme pie is a...
pie is a...
dessert!
It's not exactly perfect, but there you go..
you could also call it __super and then you don't have to put the
classnames in the __init__ (because python does it for you for
variables starting with [and not ending with] double underscores):
class Pie(Dessert):
__super = Dessert
def __init__(self):
print "pie is a..."
self.__super.__init__(self)
class BananaCremePie(Pie):
__super = Pie
def __init__(self):
print "banana creme pie is a..."
self.__super.__init__(self)
Cheers,
- Michal
------------------------------------------------------------------------
www.manifestation.com www.sabren.com www.linkwatcher.com www.zike.net
------------------------------------------------------------------------
More information about the Python-list
mailing list