Python Super Initialization Understanding process

Source: Internet
Author: User

Python's method of calling the parent class can be implemented in two ways: by calling the parent class construction method, or by using the Super function (do not mix them).

The Code "super (Foochild,self). __init__ (xxx,xxx)" often appears when using "super". So how does super () __init__ () really work?

To facilitate understanding, I wrote the following example to verify usage:

#-*-Coding:utf-8-*-
Class A (object): def __init__ (self,xing,gender): #! #1 self.namea= "AAA" #! #2 self.xing = xing #! #3 Self.gender = Gender #! #4 def Funca (self): print "function A:%s"%self.namea class B (a): def __init__ (self,xing,age): #! #5 Super (b,self) __init__ (xing,age) #! #6 (age should be gender) self.nameb= "BBB" #! #7 # #self. namea= "CCC" #! #8 # #self. xing = Xing.upper () #! #9 self.age = Age #! #10 def FUNCB (self): print "function B:%s"%self.nameb b=b ("Lin") #! #11print b.namebprint b.nameaprint b.xing #! #12print b.age #! #13b. FUNCB () B.funca ()

Mark #! by changing the code #几行的输入参数来验证super的用法, and draw the following conclusions:

The 1.B class inherits Class A, and on the basis of Class B it can call the Class A method, and the last two lines of code B.FUNCB () \b.funca () can run normally.

2.A, B also owns __init__,b will rewrite a in the __init__ method, a class method invalidation, by the comment (#) off #! #6行代码, recover #89 Line code, can be run after.

The 3.super function can call a property in a parent class, such as an attribute with the same name in the Namea, Xing,b class, overriding the same name attribute in Class A.

The 4.B class __init__ (#5) parameter needs to be greater than or equal to the __init__ method of a parent class, because Super initializes, and the number of arguments is the amount of the parent parameter.

5.super function principle: the XXX parameter in super (). __INIT__ (XXX,XXX) is a parameter entered in Class B (#5), but corresponds to the name of the parameter in Class A. As in the above code, you enter the age parameter, although the result is correct (because it is not running. gender), but in fact the age parameter is assigned to the Class A gender parameter (the second code at the end of this article has a relevant example, code add # # #处)

6. When using the Super function, you can write the same parameters in the B subclass (#5) and Class A into the xxx parameter of __init__ (XXX) (note that the self parameter is passed in super (), and it will pass implicitly in __init__ (), no need to write or write) , then you can rewrite the Parameters property in XXX again, #5中多余参数可自定义属性

Class A (object):    def __init__ (self,xing,gender):        self.namea= "AAA"        self.xing = xing        self.gender = Gender            def Funca (self):        print "function A:%s"%self.namea class B (a):    def __init__ (self,xing,gender,age):        Super (B,self) __init__ (xing,age)        self.nameb= "BBB"        # #self. namea= "CCC"        self.xing = Xing.upper ()                Self.age = age + 1        #self. Gender = Gender.upper ()            def FUNCB (self):        print "function B:%s"%self.nameb B=b ("Lin", "Nan", page) print B.namebprint b.nameaprint b.xingprint b.ageb.funcb () B.funca () print B.gender   #####

Output Result:

Bbb
Aaa
LIN
23
function b:bbb
function a:aaa
22 #####

Python Super Initialization Understanding process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.