Role Of super() in Python🤔
SUPER() The super() builtin returns a proxy object (temporary object of the superclass) that allows us to access methods and other properties of the base class. |
Example 1 :
class Person: From the above program, we can see that we have used the instances of the parent class in the child class using the super keyword, like this we can also call methods that defined inside the parent class into child class. Example 2 : From above example we can conclude that we can access all Instance variables and methods inside child class Instance method with the help of super(). Things to be remembered while using super() 1. Child class --> not allowed to access instance variables(parent class) by using super() Example class Parent: def __init__(self): self.city='Chennai' class Child(Parent): super().__init__() print(self.city) >> AttributeError: 'super' object has no arguments Error raised because we cannot access instance variable and instance method/ constructor of parent class inside child class. To avoid this we need to use super inside child class instance method/constructor. class Parent: def __init__(self): self.city='Chennai' class Child(Parent): def __init__(self): super().__init__() print(self.city) c1 = Child() >> Chennai # now the error is resolved 2. child class constructor and instance method --> access instance, class, static methods Example class Parent: def __init__(self): print('parent constructor') def m1(self): print('parent instancemethod') @classmethod def m2(cls): print('Parent classmethod') @staticmethod def m3(): print('Parent staticmethod') class Child(Parent): def __init__(self): # Child class constructor super().__init__() super().m1() super().m2() super().m3() def m4(self): # Child class Instance method super().__init__() super().m1() super().m2() super().m3() a = Child() >> parent constructor
parent instancemethod
Parent classmethod
Parent staticmethod a . m4() >> parent constructor
parent instancemethod
Parent classmethod
Parent staticmethod 3. child class, class method --> cannot access parent class instance methods and constructors by using super() directly . But by indirect means we can access it. class Parent: def __init__(self): print('parent constructor') def m1(self): print('parent instancemethod') @classmethod def m2(cls): print('Parent classmethod') @staticmethod def m3(): print('Parent staticmethod') class Child(Parent): @classmethod def m5(cls): super().__init__() #constructor super().m1() # instance method super().m2() super().m3() a = child() a.m5() >> TypeError: __init__() missing 1 required positional argument: 'self' class Child(Parent): @classmethod def m5(cls): super(Child,cls).__init__(cls) #constructor super(Child,cls).m1(cls) # instance method super().m2() super().m3() a = Child() a . m5() >> parent constructor
parent instancemethod
Parent classmethod
Parent staticmethod Thus by calling indirectly we can access instance method/constructor of parent class inside child class, class method . 4. Child class static method --> we cannot access parent class instance methods and constructors by using super() both by directly or indirectly. class Parent: def __init__(self): print('parent constructor') def m1(self): print('parent instancemethod') @classmethod def m2(cls): print('Parent classmethod') @staticmethod def m3(): print('Parent staticmethod') class Child(Parent): @staticmethod def m5(cls): super(Child,Child).__init__() #constructor super(Child,Child).m1() # instance method super().m2() super().m3() a = child() a.m6() >> TypeError: __init__() missing 1 required positional argument: 'self' Thus we cannot access instance method/constructor of parent class inside static method of child class directly or indirectly. Only static and class methods of parent can be access inside child class static method class Child(Parent): @staticmethod def m5(cls): super().m2() super().m3() a = child() a.m6() >> |
Comments
Post a Comment