tags:

views:

158

answers:

4

As the title says, how do you remember the order of super's arguments? Is there a mnemonic somewhere I've missed?

After years of Python programming, I still have to look it up :(

(for the record, it's super(Type, self))

+3  A: 

I don't. In Python 3 we can just write

super().method(params)
KennyTM
Interesting. Still on Python 2.6 so have not seen this yet (although I'm not really sure at the moment if I think this `looks` more elegant / correct)
ChristopheD
Ah, I hadn't realized. Interesting — thanks.
David Wolever
+5  A: 

Inheritance makes me think of a classification hierarchy. And the order of the arguments to super is hierarchical: first the class, then the instance.

Another idea, inspired by the answer from ~unutbu:

class Fubb(object):
    def __init__(self, *args, **kw):
        # Crap, I can't remember how super() goes!?

Steps in building up a correct super() call.

__init__(self, *args, **kw)              # Copy the original method signature.

super(Fubb).__init__(self, *args, **kw)  # Add super(Type).
                     /
              -------
             /
super(Fubb, self).__init__(*args, **kw)  # Move 'self', but preserve order.
FM
Oh boy, that's perfect. Thanks.
David Wolever
+6  A: 

Simply remember that the self is optional - super(Type) gives access to unbound superclass methods - and optional arguments always come last.

THC4k
Ah, also interesting — I hadn't realized that.
David Wolever
+1  A: 

Typically, super is used inside of a class definition. There, (again typically), the first argument to super should always be the name of the class.

class Foo(object):
    def __init__(self,*args,**kw):
        super(Foo,self).__init__(*args,**kw)
unutbu