Sunday, April 29, 2018

Python : Super() vs Self

Python : Super() vs Self vs Classname

All 3 can be differentiated wrt to usage inside th child class. 

General Idea:
  1. Assume Class A , having a method m1
  2. Class B  is child of Class A 
  3. Class B has methods m1 ,m2
  4. If you want to call parent method m1 (ie., A().m1()) inside m1,m2 in Class B and use self , it will call the already exsisting method m1() in the same Class B  .
  5. Hence we use super() so that you are specifying python to use parent method.


super()
1.  Used inside child constructor to call parent constructor
2.  Used inside overriding method to call the parent method

self
1. Used to call method and properties of the same class
2. Used to call methods and properties of parent class


className
1. Can be used to call properties and methods of parent class.
2. Parent class name needs to be hardcoded
3. "self" needs to be sent as an arguement.

1.Mainly used inside child constructor to call parent constructor.
https://www.digitalocean.com/community/tutorials/understanding-class-inheritance-in-python-3
class Bird():
    name=None
    def __init__(self,name):
        self.name=name
    def bird_name(self):
        return(self.name)
    def fly(self):
        return("True")

class non_fly(Bird):
    swim=None
    def __init__(self,name,swim):   #Constr overriding
        super().__init__(name)      #call parent constr

       #  Bird.__init__(self, name) 
        self.swim=swim
    def fly(self):                  #Method Overrdiing
        return("false")

peng=non_fly("peng",True)
print(peng.bird_name(),peng.fly(),peng.swim)#inherit

2. Used in overriding method to class the parent method

class a():
    def m(self):
        return("m")


class b(a):       
    def m(self):
        #print(self.m()) #error - infinite loop case as it means m() in class b.
        print(a.m(self)) # here class name is hardcoded and need to send "self"  
        print(super().m()) #BETTER OPTION
B=b()
B.m() 


O/P:
m

 

3. MRO

Ref:
Python - MRO(Method Resolution Order)
https://stackoverflow.com/questions/30041679/python-self-and-super-in-multiple-inheritance


class a():
    def x(self):
        return("a")  
class b():
    def x(self):
        return("b")
class c(a):
    def s(self):
        print(super().x(),self.x())  #a,b

class d(b,c):
    pass
d().s()

print(d.mro()) 
#(<class '__main__.d'>, <class '__main__.b'>, <class '__main__.c'>, <class '__main__.a'>, <class 'object'>)

Note :
1. d().s()
2. goes to s() in class c
3. super() = wrt current class ie., super() of class c = class a .Therefore "a"
4. self - check if 1st parent has mentioned method , if not it goes to next parent .There class b has method x. Therefore "b"

12 comments:

  1. Best RPA Online Training institute `
    Twitter URL:- https://twitter.com/supreetsolution
    Facebook URL:https://www.facebook.com/rpaonlinetraininginhyd/
    Instagram URL: https://www.instagram.com/supreetsolutions/
    Pinterest URL: https://www.pinterest.com/supreetsolutions/
    Linkedin URL: https://www.linkedin.com/company/supreetsolutions/?originalSubdomain=in
    Google+: https://plus.google.com/115783164183222120736

    ReplyDelete
  2. Click Here Best RPA Online Training to go to RPA Online Training Institute Details.

    ReplyDelete
  3. I'm here representing the visitors and readers of your own website say many thanks for many remarkable
    python training in chennai
    python training in Bangalore
    Python training institute in chennai

    ReplyDelete
  4. I am really enjoying reading your well written articles.
    It looks like you spend a lot of effort and time on your blog.
    I have bookmarked it and I am looking forward to reading new articles. Keep up the good work..
    Hadoop Training in Chennai
    Big Data Training in Chennai
    hadoop training in bangalore
    big data training in bangalore

    ReplyDelete