Why does super take a class name as the argument?

Sion Arrowsmith siona at chiark.greenend.org.uk
Fri Oct 15 11:45:37 EDT 2004


Chris Green  <cmg at dok.org> wrote:
>I've done a bit of searching in the language reference and a couple
>pages referring the behavior of super() but I can't find any
>discussion of why super needs the name of the class as an argument.

http://www.python.org/2.2.1/descrintro.html#mro and
http://www.python.org/2.2.1/descrintro.html#cooperation

>super(self).method() seems like super could just do the right
>thing...
>
>super(kls).method() could also work if you needed access up the tree
>to from a classmethod. Maybe I'm missing something with Metaclasses.

To try and summarise (and grossly simplify), consider:

class A(object):
    def method(self):
        pass

class B(A):
    def method(self):
        super(B, self).method()

class C(A):
    def method(self):
        super(C, self).method()

class D(B, C):
    def method(self):
        super(A, self).method()
        
If super worked just off self, how are the method()s in B and C
supposed to know that they should be calling A.method() (since
isinstance(self, D))? If super worked just off the class, both
B.method() and C.method() would call A.method() when called
from D.method(). So super needs to know both the class of self
and the class the method it is being used from belongs to. And
if you think the latter is easy for the compiler, consider

def generic_method(self):
    ...

B.method = generic_method

-- 
\S -- siona at chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |    -- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump



More information about the Python-list mailing list