码迷,mamicode.com
首页 > 其他好文 > 详细

类属性增删查改

时间:2019-03-20 19:24:26      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:报错   color   art   int   code   %s   lex   增删查改   china   

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

 

类属性增删查改

标签:报错   color   art   int   code   %s   lex   增删查改   china   

原文地址:https://www.cnblogs.com/wuweixiong/p/10566727.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!