__init__ method in python

init has two major () method sense of reason. The first reason is to initialize the object life cycle is the most important step; after each object must be initialized properly to work properly. The second reason is the init () parameter value may take many forms.

__init__ method

 1. Using the demo

initialization. Note that the start and end of the name are double underlined.
__Init__ method using the
code examples

Copy the code
#!/usr/bin/python
# Filename: class_init.py
class Person:
    def __init__(self, name):
        self.name = name
    def sayHi(self):
        print Hello, my name is, self.name
p = Person(Swaroop)
p.sayHi()
# This short example can also be written as Person(Swaroop).sayHi()
Copy the code

 

Export

$ python class_init.py
Hello, my name is Swaroop

 

How It Works
Here, we define the __init__ method as taking a parameter name (and common parameter self). In this __init__, we just create a new field also called name. Note that they are two different variables even though they have the same name. No point allows us to distinguish them.

The most important thing is, we did not specifically call the __init__ method, just create a new instance of the class when the parameters include the class name in the back with, thus passed to the __init__ method in parentheses. This is an important point of this approach.

Now, we can use self.name domain in our approach. This has been verified sayHi process.
       __init__ method is similar to C, C # and Java in the constructor 

2. Notes

 

 

 

Note 1, __ init__ is not equivalent to a constructor in C #, it's time to perform, the examples have been constructed.

class A(object):
    def __init__(self,name):
        self.name=name
    def getName(self):
        return 'A '+self.name

 

When we perform

a=A('hello')

 

When, it can be understood as

a=object.__new__(A)
A.__init__(a,'hello')

 

__Init__ i.e. after initialization function is the object instantiated.

 

Note 2, subclasses can not override the __init__, instance subclasses, will automatically call the superclass __init__ defined

Copy the code
class B(A):
    def getName(self):
        return 'B '+self.name
 
if __name__=='__main__':
    b=B('hello')
    print b.getName()
Copy the code

 

However, if the rewrite __init__, instance subclasses, it will not go __init__ implicit call the super class has been defined

Copy the code
class C(A):

    def __init__(self):

        pass

    def getName(self):

        return 'C
 '+self.name

 

if __name__=='__main__':

    c=C()

    print c.getName()

 
Copy the code

 

It will be reported "AttributeError: 'C' object has no attribute 'name'" error, so if you override the __init__, in order to use or extend the superclass behavior, the best explicitly call the superclass __init__ method

Copy the code
class C(A):

    def __init__(self,name):

        super(C,self).__init__(name)

    def getName(self):

        return 'C
 '+self.name

 

if __name__=='__main__':

    c=C('hello')   


    print c.getName()
Copy the code
 

 

 

Reference link: https: //www.cnblogs.com/insane-Mr-Li/p/9758776.html

Guess you like

Origin www.cnblogs.com/xiohao/p/11280330.html