as far as i know , in OO a subclass inherits all attributes of it's parent. but in python a subclass won't inherit it's parent's attributes until it calls it's parent's init method in this way : super().init() . I wanted to know is my deduction correct ? and why is it like that ? i guess it other languages like java a subclass automatically inherits all attributes of it's parent and there is no need to do such things . thank you in advance.
-
Does this answer your question? [Understanding Python super() with \_\_init\_\_() methods](https://stackoverflow.com/questions/576169/understanding-python-super-with-init-methods). Basically the point is to ensure that the next method in the Method resolution order (MRO) is called. – Aditya Jul 19 '20 at 05:32
-
1A python *subclass* does inherit the parent *class attributes*. But *instance attributes* always belong to *particular instances* in Python, so it doesn't make sense to inherit them. E.g. `class Foo: pass; foo = Foo(); foo.a = 'bar'`, Then `class Baz(Foo): pass`, should `Baz()` have an `a` attribute? – juanpa.arrivillaga Jul 19 '20 at 06:11
1 Answers
When you call the super().__init__()
within a subclass, you're just executing the parents init method and everything in that method.
The child class will inherit the attributes and all the methods without calling the super().__init__()
function. However, if you defined an init function within the child class, then it will overwrite the copied parents class init method within the child class. You have to remember that when a child class inherits a parent class, it essentially makes a child class that is a duplicate of the parent class, which can be added to and/or altered without affecting the parent class. You can pick and choose which those things you want the child class to inherit from the parent by not overwriting them, and you can choose which of those you don't want to inherit by overwriting them.
Example 1
class parentclass:
b = 31
def __init__(self):
self.a = 21
def add(self, a, b):
c = a+b
print(c)
class childclass(parentclass):
pass
child1 = childclass()
child1.add(10,20)
print(child1.b)
print(child1.a)
Output:
30
31
21
Example 2:
class parentclass:
b = 31
def __init__(self):
self.a = 21
def add(self, a, b):
c = a+b
print(c)
class childclass(parentclass):
def __init__(self):
self.name="child"
child1 = childclass()
child1.add(10,20)
print(child1.b)
print(child1.a)
Output:
Traceback (most recent call last):
File "C:/Users/chees/PycharmProjects/untitled/idk.py", line 20, in <module>
print(child1.a)
AttributeError: 'childclass' object has no attribute 'a'
30
31
Edit:
Which is why the super().init() method is required within the child's init method, in order to copy the parents init method within the childs init method again, i.e:
class parentclass:
b = 31
def __init__(self):
self.a = 21
def add(self, a, b):
c = a+b
print(c)
class childclass(parentclass):
def __init__(self):
super().__init__()
self.name="child"
child1 = childclass()
child1.add(10,20)
print(child1.b)
print(child1.a)
is the same as
class parentclass:
b = 31
def __init__(self):
self.a = 21
def add(self, a, b):
c = a+b
print(c)
class childclass(parentclass):
def __init__(self):
self.a = 21
self.name="child"
child1 = childclass()
child1.add(10,20)
print(child1.b)
print(child1.a)

- 183
- 8