Python-oriented knowledge and instantiation of object-specific (object)

Source: Internet
Author: User

1, the object is an instance, what is an instance

The process of running a class is the process of instantiation, and the result of instantiation is that it produces an instance

The thing about class is that without a manual return, he will load the function and automatically return to the dictionary inside the function, which encapsulates the data attributes in the dictionary __init__.

A function property is a class-owned

2. Instantiation

As long as the class name plus () run, (in the class this run called instantiation) will first run __init__ this function

classChinese:#defines the class of Chinesedang="When"#defines a data property dang    def __init__(self,name,age,gender):Print("I was initializing the function and I started to run the") Self.mingzi=name Self.nainji=Age Self.xignbie=gender#this will return automatically when you finish writing .        Print("I'm done.")    defSuiditutan ():#defines a function property        Print("To the wall is a mouthful of yellow phlegm")    defChadui (self):#also defines a function property        Print("%s is plugged in to the front"%Self ) P1=chinese ("North Sir", 33,"female") C:\python35\python3.exe D:/pyproject/day24/object-related knowledge. PY I'm the initialization function and I'm starting to run I'm finished

The process of instantiation is to run the __init__ function, thus generating a dictionary, which is the instance (object)

The data attributes are encapsulated in the dictionary

View the contents of this instance under P1

classChinese:#defines the class of Chinesedang="when"#defines a data property dang    def __init__(self,name,age,gender):Print("I was initializing the function and I started to run the") Self.mingzi=name Self.nainji=Age Self.xignbie=gender#this will return automatically when you finish writing .        Print("I'm done.")    defSuiditutan ():#defines a function property        Print("To the wall is a mouthful of yellow phlegm")    defChadui (self):#also defines a function property        Print("%s is plugged in to the front"%Self ) P1=chinese ("North Sir", 33,"female")Print(P1.__dict__) C:\python35\python3.exe D:/pyproject/day24/object-related knowledge. PY I was initializing the function, and I started to run it out of the end of my {'Nainji': 33,'Xignbie':'female','Mingzi':'North Sir'}

Note: Do not have return add in __init__ inside will error, class will automatically help you return

3. Instance properties can be accessed to class properties

classChinese:#defines the class of Chinesedang="when"#defines a data property dang    def __init__(Self,name,age,gender):#Self is the P1 itself .        Print("I was initializing the function and I started to run the") Self.mingzi=name#P1.mingzi=nameSelf.nianji=age#P1.nainji=ageSelf.xingbie=gender#this will return automatically when you finish writing .        Print("I'm done.")    defSuiditutan ():#defines a function property        Print("To the wall is a mouthful of yellow phlegm")    defChadui (self):#also defines a function property        Print("%s is plugged in to the front"%Self ) P1=chinese ("North Sir", 33,"female")Print(P1.__dict__)#There are only data attributes in the dictionary.Print(P1.__dict__["Nianji"])#use dictionary to fetch value from dictionaryPrint(P1.__dict__["Mingzi"])Print(P1.nianji)#instance Call NianjiPrint(P1.mingzi)#This is the data property of the instance#print (p1.mingzi222) #会报错, when the instance calls the method will be from the __dict__ inside to find, can not find the error.Print(P1.dang)#use the instance to invoke the data property of the class, although there is no __dict__ inside, but the upper level hasC:\python35\python3.exe D:/pyproject/day24/object-related knowledge. PY I was initializing the function, and I started to run it out of the end of my {'Nianji': 33,'Xingbie':'female','Mingzi':'North Sir'}33North Sir33bei ye

Instance in the invocation of properties, the first instance of the property dictionary, if not, go to the class of the property dictionary inside, the class in the absence of words on the error

4, class call method directly with the class name plus ()

classChinese:#defines the class of Chinesedang="when"#defines a data property dang    def __init__(Self,name,age,gender):#Self is the P1 itself .        Print("I was initializing the function and I started to run the") Self.mingzi=name#P1.mingzi=nameSelf.nianji=age#P1.nainji=ageSelf.xingbie=gender#this will return automatically when you finish writing .        Print("I'm done.")    defSuiditutan ():#defines a function property        Print("To the wall is a mouthful of yellow phlegm")    defChadui (self):#also defines a function property        Print("%s is plugged in to the front"%Self.mingzi) P1=chinese ("North Sir", 33,"female") Chinese.suiditutan () Chinese.chadui (p1) C:\python35\python3.exe D:/pyproject/day24/object-related knowledge. PY I was the initialization function and I started running I ended up moving towards the wall is a mouthful of yellow phlegm.

5. Invoking the method with an instance

classChinese:#defines the class of Chinesedang="when"#defines a data property dang    def __init__(Self,name,age,gender):#Self is the P1 itself .        Print("I was initializing the function and I started to run the") Self.mingzi=name#P1.mingzi=nameSelf.nianji=age#P1.nainji=ageSelf.xingbie=gender#this will return automatically when you finish writing .        Print("I'm done.")    defSuiditutan (self):#defines a function property        Print("%s is a yellow sputum on the wall."%Self.mingzi)defChadui (self):#also defines a function property        Print("%s is plugged in to the front"%Self.mingzi) P1=chinese ("North Sir", 33,"female") Chinese.suiditutan (p1)#Class Call MethodChinese.chadui (p1)#Class Call MethodP1.chadui ()#instance calls the method and automatically passes the P1 to selfP1.suiditutan ()#instance calls the method and automatically passes the P1 to selfC:\python35\python3.exe D:/pyproject/day24/object-related knowledge. PY I was the initialization function, I started to run I ended up the north Sir toward the wall is a mouthful of yellow phlegm North Ye inserted in front of the North Ye inserted in front of the North Ye to the wall is a mouthful of yellow phlegm

6, then instantiate a P2 to add a class of function properties, call with an instance

classChinese:#defines the class of Chinesedang="when"#defines a data property dang    def __init__(Self,name,age,gender):#Self is the P1 itself .Self.mingzi=name#P1.mingzi=nameSelf.nianji=age#P1.nainji=ageSelf.xingbie=gender#this will return automatically when you finish writing .    defSuiditutan (self):#defines a function property        Print("%s is a yellow sputum on the wall."%Self.mingzi)defChadui (self):#also defines a function property        Print("%s is plugged in to the front"%Self.mingzi)defEatfood (self,food):Print("%s is eating%s"%(Self.mingzi,food)) P1=chinese ("North Sir", 33,"female") P1.eatfood ("Yellow phlegm") P2=chinese ("Alex", 28,"female") P2.suiditutan ()#the instance invokes the function property of the class and automatically passes the P1 to selfP2.eatfood ("braised chicken and rice with yellow sauce")#the instance invokes the function property of the class and automatically passes the P1 to selfC:\python35\python3.exe D:/pyproject/day24/object-related knowledge. Py North ye are eating yellow phlegm meow, the head toward the wall is a mouthful of yellow phlegm meow is eating yellow stewed chicken rice

Python-oriented knowledge and instantiation of object-specific (object)

Related Article

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.