Python Object-oriented

Source: Internet
Author: User

Two usages of class and object classes
1 #!/usr/bin/python2 #-*-coding:utf-8-*-3 4 5 classChinese:6Country =' China'7 8     def __init__(self,name,age):9Self.name =nameTenSelf.age = Age One  A     defTalk (self): -         Print('say Chinese', Self.name) -  the  - #first usage of the class: instantiation -P1 = Chinese ('Egon', 18) - P1.talk () + Print(P1.name) - Print(p1.age) +  A #second usage of class: attribute reference (add and revise) at Print(Chinese.country)#the data properties of the class - Print(Chinese.__init__)#function Properties of the class -Chinese.__init__(a)#Error -  - Print(Chinese.__dict__)#view the class's property dictionary, or namespace - Print(chinese.country) in Print(Chinese.__dict__['Country']) -  to #types are unified with classes +P1 = Chinese ('Egon', 18) -P2 = Chinese ('Alex', 1000) the Print(ID (p1.country)) * Print(ID (p2.country)) $ Print(ID (chinese.country))Panax Notoginseng  - Print(Chinese.talk) the Print(P1.talk) + Print(P2.talk) A  the Print(p1) + P1.talk () - Print(p2) $ P2.talk () $ Chinese.talk (p1) -  - #variables defined inside a class are common to all objects and have the same ID . the #A function defined inside a class, bound to all objects, is used for the object, and Obj.func () will use obj itself as the first argument . - Wuyi #binding method: Bind to WHO, is to who use, who call will automatically put themselves as the first parameter passed in
View Code

Python Object-oriented

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.