类属性增删查改
Posted wuweixiong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了类属性增删查改相关的知识,希望对你有一定的参考价值。
class Chinese: country=‘China‘ def __init__(self,name): self.name=name def play_ball(self,ball): print(‘%s 正在打 %s‘ %(self.name,ball)) #查看 print(Chinese.country) #China #修改 Chinese.country=‘Japan‘ print(Chinese.country) #Japan p1=Chinese(‘alex‘) print(p1.__dict__) #{‘name‘: ‘alex‘} print(p1.country) #Japan #增加 Chinese.party=‘共产_党‘ print(Chinese.party) #共产_党 print(p1.party) #共产_党 #删除 del Chinese.party del Chinese.country # print(Chinese.__dict__) # print(Chinese.country) #报错:AttributeError: type object ‘Chinese‘ has no attribute ‘country‘ #增加函数属性 def eat_food(self,food): print(‘%s 正在吃%s‘ %(self.name,food)) Chinese.eat=eat_food #增加了一个函数名为eat的函数属性 print(Chinese.__dict__) p1.eat(‘麻辣烫‘) # {‘__module__‘: ‘__main__‘, ‘__init__‘: <function Chinese.__init__ at 0x01055FA8>, ‘play_ball‘: <function Chinese.play_ball at 0x02C13030>, ‘__dict__‘: <attribute ‘__dict__‘ of ‘Chinese‘ objects>, ‘__weakref__‘: <attribute ‘__weakref__‘ of ‘Chinese‘ objects>, ‘__doc__‘: None, ‘eat‘: <function eat_food at 0x01055F60>} # alex 正在吃麻辣烫 #修改函数属性 def test(self): print(‘test‘) Chinese.play_ball=test p1.play_ball() #test
以上是关于类属性增删查改的主要内容,如果未能解决你的问题,请参考以下文章