Python之类属性的增删改查

#类属性又称为静态变量,或者是静态数据,这些数据是他们所属的类对象绑定的,不依赖于任何类实例

class ChinesePeople:
country = 'china'
def __init__(self,name):
self.name = name
def play_ball(self,ball):
print ("%s 正在打 %s" %(self.name))
def say_word(self,word):
print ("%s 说 %s" %(self.name,word))

#查看类属性
print (ChinesePeople.country)
#修改类属性
ChinesePeople.country = "CHINA"
print ChinesePeople.country
#删除类属性
del ChinesePeople.country
#增加类属性
ChinesePeople.country = "china"
ChinesePeople.location = "Asia"
print (ChinesePeople.__dict__)

猜你喜欢

转载自www.cnblogs.com/wangyue0925/p/8979486.html