关于__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'}