python学习-62 类属性的增删该查 – Python量化投资

python学习-62 类属性的增删该查

                                                                             类属性

 

 

1.类属性

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

 

2.增 删 改 查


class zoo:
    country = 'china'
    def __init__(self,name,address,kind):
        self.name = name
        self.address = address
        self.kind = kind
    def monkey(self):
        print('this is monkey (%s)' %self.address)
    def tiger(self):
        print('this is tiger (%s)' %self.kind)
    def end(self):
        print('welcome to %s' %self.name)
dwy = zoo('chinese\'s zoo','beijing','others')

zoo.monkey(dwy)
zoo.tiger(dwy)
zoo.end(dwy)

# 对类的属性增 删 改 查

print(zoo.country)                  #  查看信息

zoo.country = 'US'                # 修改信息
print(zoo.country)


zoo.snake = 'this is snake'          # 增加
print(dwy.snake)


del zoo.country                 # 删除
del zoo.snake

print(zoo.__dict__)


 

运行结果:


this is monkey (beijing)
this is tiger (others)
welcome to chinese's zoo
china
US
this is snake
{'__module__': '__main__', '__init__': <function zoo.__init__ at 0x7fb30efb8d90>, 'monkey': <function zoo.monkey at 0x7fb30efb8f28>, 'tiger': <function zoo.tiger at 0x7fb30efc9048>, 'end': <function zoo.end at 0x7fb30efc9378>, '__dict__': <attribute '__dict__' of 'zoo' objects>, '__weakref__': <attribute '__weakref__' of 'zoo' objects>, '__doc__': None}

Process finished with exit code 0


 

https://www.cnblogs.com/liujinjing521/p/11457902.html

「点点赞赏,手留余香」

    还没有人赞赏,快来当第一个赞赏的人吧!
Python
0 条回复 A 作者 M 管理员
    所有的伟大,都源于一个勇敢的开始!
欢迎您,新朋友,感谢参与互动!欢迎您 {{author}},您在本站有{{commentsCount}}条评论