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 inView Code
Python Object-oriented