Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how does a subclass inherits a parent's attributes in python

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.

like image 488
mohsen_true Avatar asked Nov 29 '25 08:11

mohsen_true


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)
like image 84
Pokebab Avatar answered Dec 01 '25 20:12

Pokebab