patternpythonCritical
Understanding Python super() with __init__() methods
Viewed 0 times
pythonunderstandingmethodssuper__init__with
Problem
Why is
Is there a difference between using
super() used?Is there a difference between using
Base.__init__ and super().__init__?class Base(object):
def __init__(self):
print "Base created"
class ChildA(Base):
def __init__(self):
Base.__init__(self)
class ChildB(Base):
def __init__(self):
super(ChildB, self).__init__()
ChildA()
ChildB()Solution
super() lets you avoid referring to the base class explicitly, which can be nice. But the main advantage comes with multiple inheritance, where all sorts of fun stuff can happen. See the standard docs on super if you haven't already.Note that the syntax changed in Python 3.0: you can just say
super().__init__() instead of super(ChildB, self).__init__() which IMO is quite a bit nicer. The standard docs also refer to a guide to using super() which is quite explanatory.Context
Stack Overflow Q#576169, score: 2378
Revisions (0)
No revisions yet.