1. Properties of the class :
#/usr/bin/env#coding = Utf-8classChinese:#The properties of the class are in thisCountry =' China' def __init__(self, name):#The properties of the instance are in thisSelf.name =namedefPlay_ball (self, Ball):Print('%s is playing%s'%(self.name, ball))#View PropertiesPrint(chinese.country)#Modifying Propertieschinese.country='Japan'Print(chinese.country) P1=chinese ('Alex')Print(P1.__dict__)Print(p1.country)#Add Propertychinese.dang='Party'Print(Chinese.dang)Print(P1.dang)#Delete PropertydelChinese.dangdelChinese.countryPrint(Chinese.__dict__)#print (chinese.country)results: chinajapan{'name':'Alex'}japan Party Party {'__module__':'__main__','__init__': <function Chinese.__init__At 0x0000028b584a66a8>'Play_ball': <function Chinese.play_ball at 0x0000028b584a6620>, '__dict__': <attribute'__dict__'Of'Chinese'Objects>,'__weakref__': <attribute'__weakref__'Of'Chinese'Objects>,'__doc__': None} adds function properties:#Define a functiondefEat_food (self,food):Print('%s is eating%s'%(Self.name,food))#give this function to the classchinese.eat=Eat_foodPrint(Chinese.__dict__) P1.eat ('Excrement')#TestdefTest (self):Print('Test') Chinese.play_ball=Testp1.play_ball ()
Results:
Alex is eating the crap test.
2. Properties of the instance:
#/usr/bin/env#coding = Utf-8classChinese:country=' China' def __init__(self,name): Self.name=namedefPlay_ball (self,ball):Print('%s is playing%s'%(Self.name,ball)) P1=chinese ('Alex')Print(P1.__dict__)#IncreaseP1.age=18Print(P1.__dict__)Print(p1.age)#do not modify the underlying property dictionary#p1.__dict__[' sex ']= ' male '#print (p1.__dict__)#print (p1.sex)#ModifyP1.age=19Print(P1.__dict__)Print(P1.age)
Results:
{' name ': ' Alex '} {' name ': ' Alex ', ' age ': +}18{' name ': ' Alex ', ' age ':}19
Python object-oriented grooming--------1. Classes and Instance Properties