super - is (should) it (be) a reserved word?

jay.krell at cornell.edu jay.krell at cornell.edu
Tue Oct 10 05:25:42 EDT 2000


What do the leading underscores do? In my example I was sure to use three
levels because when I had tried to figure out something like this in the
past the more derived super's kept replacing the less derived ones, so it
didn't work. I couldn't find that the underscores accomplished anything.

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();

Hm..actually, ok, I removed the __. That makes Python crash, stack overflow.
I couldn't find what this is doing in the docs. Looking through the code, _
seemed used mainly to preflight string comparisons against strings that
start (and end) with double _.

Anyway, cool, thanks, I wanted this in Python a few weeks ago and couldn't
figure out how to do it.

 - Jay

-----Original Message-----
From: Christian Tanzer <tanzer at swing.co.at>
To: python-list at python.org <python-list at python.org>
Date: Monday, October 09, 2000 11:31 PM
Subject: Re: super - is (should) it (be) a reserved word?



Michal Wallace <sabren at manifestation.com> wrote:

> 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)

If you do that, then you'd better not call one of your classes `_X'
and one of its descendent classes `X' -- if you do, you'll get a name
clash between `_X.__super' and `X.__super'. That name clash leads to
infinite recursion in turn.

Cheers,
Christian

PS: Is that a bug or a feature?

--
Christian Tanzer                                         tanzer at swing.co.at
Glasauergasse 32                                       Tel: +43 1 876 62 36
A-1130 Vienna, Austria                                 Fax: +43 1 877 66 92


--
http://www.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list