Say I have the folowing code:
class Class1(object):
def __init__(self):
self.my_attr = 1
self.my_other_attr = 2
class Class2(Class1):
def __init__(self):
super(Class1,self).__init__()
Why does Class2 not inherit the attributes of Class1?
JoseK
31.4k14 gold badges109 silver badges135 bronze badges
asked Jun 16, 2010 at 12:36
Jim Jeffries
10.1k16 gold badges66 silver badges105 bronze badges
2 Answers 2
You used super wrong, change it to
super(Class2, self).__init__()
Basically you tell super to look above the given class, so if you give Class1 then that __init__ method is never called.
answered Jun 16, 2010 at 12:40
nikow
21.6k7 gold badges54 silver badges71 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Because you're giving super the wrong class. It should be:
class Class2(Class1):
def __init__(self):
super(Class2,self).__init__()
answered Jun 16, 2010 at 12:42
detly
30.6k20 gold badges101 silver badges161 bronze badges
Comments
lang-py
superis more like part of the solution. Picking onsuperis like saying that seatbelts are bad because you can still get hurt in an accident. You are generally far better of if you usesuper.