Python: how do you remember the order of `super`'s arguments?
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)
)
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.
Simply remember that the self
is optional - super(Type)
gives access to unbound superclass methods - and optional arguments always come last.
I don't. In Python 3 we can just write
super().method(params)
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)
精彩评论