关于__dict__
Posted kerryworld
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于__dict__相关的知识,希望对你有一定的参考价值。
class A(object): class_var = 1 def __init__(self): self.name = ‘xy‘ self.age = 2 #@property def num(self): return self.age + 10 def fun(self):pass def static_f():pass def class_f(cls):pass if __name__ == ‘__main__‘:#主程序 a = A() print (a.__dict__) #{‘age‘: 2, ‘name‘: ‘xy‘} 实例中的__dict__属性 print (A.__dict__) a.addr=‘ssdd‘ #新增一个元素addr print(a.__dict__) a.num=10 print(a.num) print(a.__dict__)
运行结果:
{‘name‘: ‘xy‘, ‘age‘: 2} {‘__doc__‘: None, ‘__module__‘: ‘__main__‘, ‘static_f‘: <function A.static_f at 0x00000264AC4D0620>, ‘class_f‘: <function A.class_f at 0x00000264AC4D06A8>, ‘class_var‘: 1, ‘num‘: <function A.num at 0x00000264AC4D0510>, ‘__init__‘: <function A.__init__ at 0x00000264AC4D0158>, ‘fun‘: <function A.fun at 0x00000264AC4D0598>, ‘__dict__‘: <attribute ‘__dict__‘ of ‘A‘ objects>, ‘__weakref__‘: <attribute ‘__weakref__‘ of ‘A‘ objects>} {‘name‘: ‘xy‘, ‘age‘: 2, ‘addr‘: ‘ssdd‘} 10 {‘age‘: 2, ‘num‘: 10, ‘name‘: ‘xy‘, ‘addr‘: ‘ssdd‘}
以上是关于关于__dict__的主要内容,如果未能解决你的问题,请参考以下文章