首先,得了解类的特殊属性__dict__,它是一个字典,用于存储类或者实例的属性,即使你没有去定义它,它也存在每个类中,是默认隐藏的。然后来看下面三个问题:
Q1、类属性如果重新被赋值,是否会影响到类属性的引用?
class TestA:
attr = 1
obj_a = TestA()
TestA.attr = 42
print(obj_a.attr)
print(TestA.__dict__)
print(obj_a.__dict__)
-------------------------
42
{'__module__': '__main__', 'attr': 42, '__dict__': <attribute '__dict__' of 'TestA' objects>, '__weakref__': <attribute '__weakref__' of 'TestA' objects>, '__doc__': None}
{}
Q2、实例属性如果被重新赋值,是否会影响到类属性的引用?
class TestA:
attr = 1
obj_a = TestA()
obj_b = TestA()
obj_a.attr = 42
print(obj_b.attr)
print(TestA.__dict__)
print(obj_a.__dict__)
-------------------------
1
{'__module__': '__main__', 'attr': 1, '__dict__': <attribute '__dict__' of 'TestA' objects>, '__weakref__': <attribute '__weakref__' of 'TestA' objects>, '__doc__': None}
{'attr': 42}
Q3、类属性实例属性具有相同的名称,那么.后面引用的将会是什么?
class TestA:
attr = 1
def __init__(self):
self.attr = 42
obj_a = TestA()
print(obj_a.attr)
print(TestA.__dict__)
print(obj_a.__dict__)
-------------------------
42
{'__module__': '__main__', 'attr': 1, '__init__': <function TestA.__init__ at 0x7fbd7d2217a0>, '__dict__': <attribute '__dict__' of 'TestA' objects>, '__weakref__': <attribute '__weakref__' of 'TestA' objects>, '__doc__': None}
{'attr': 42}
从结果可以知道,属性的引用机制是自外而内的,python的编译器会先搜索该实例是否拥有某个属性,如果有则引用;如果没有,将搜索这个实例所属的类是否有这个属性,如果有则引用,没有则报错。