super - is (should) it (be) a reserved word?
jay.krell at cornell.edu
jay.krell at cornell.edu
Wed Oct 11 08:39:57 EDT 2000
Sorry, no, that doesn't work. Python is too dynamic.
class Food:
def Print(self):
print("Food")
class Dessert(Food):
super = Food
def Print(self):
self.super.Print(self)
print("Dessert")
class Pie(Dessert):
super = Dessert
def Print(self):
self.super.Print(self)
print("Pie")
Pie().Print();
recurses infinitely. You were just lucky to try with only two levels of
inheritance.
If you replace all occurences of super with __super, it works, because
Python has private, kind of, I guess.
- Jay
-----Original Message-----
From: Alan Gauld <alan.gauld at gssec.bt.co.uk>
Newsgroups: comp.lang.python
To: python-list at python.org <python-list at python.org>
Date: Wednesday, October 11, 2000 3:26 AM
Subject: Re: super - is (should) it (be) a reserved word?
>Grant Edwards wrote:
>> >Wouldn't it be nice if "super" were a reserved word
>
>OK, Having read the thread so far it looks like no-one
>has suggested the C++ trick mentioned by Bjarne Stroustrup
>in his 'Design and Evolution' book, so here goes:
>
>class Foo:
> def __init__(self):
> self.a = 1
> def doit(self):
> print "a = ", self.a
>
>class Bar(Foo):
> super = Foo
> def __init__(self,n):
> self.super.__init__(self)
> self.b = n
> def doit(self):
> self.super.doit(self)
> print "b = ", self.b
>
>f = Foo()
>f.doit()
>
>b = Bar(5)
>b.doit()
>
>
>This is somewhat like using super and has the real maintenance
>advantage that if the superclass of Bar is changed you don't
>need to edit all the methods.
>
>Alan G
>--
>http://www.python.org/mailman/listinfo/python-list
More information about the Python-list
mailing list