话不多说,直接上代码:
class Parent(object):
x = 1
class Child1(Parent):
pass
class Child2(Parent):
pass
print(Parent.x, Child1.x, Child2.x)
Child1.x = 2
print(Parent.x, Child1.x, Child2.x)
Parent.x = 3
print(Parent.x, Child1.x, Child2.x)
"""
运行结果:
1 1 1
1 2 1
3 2 3
"""
我对这段代码最大的疑问是为什么最后一行的输出是 3 2 3 却不是 3 2 1 ?
原来,在Python 中,类变量在内部是作为字典处理的。如果一个变量名没有在当前类的字典中发现,将会向上搜索祖先类直到被引用的变量名被找到为止。(如果这个变量名既没有在自己所在的类又没有在祖先类中找到,会引发一个AttributeError异常 )
下面我们逐行分析一下:
- 第一行(1 1 1):父类Parent中设置了
x = 1
,当访问子类Child1和Child2的变量 x 时,因为子类Child1和Child2的变量 x 没有在当前类中找到,所以会向上引用父类Parent中的变量 x 。显示id代码如下:
class Parent(object):
x = 1
class Child1(Parent):
pass
class Child2(Parent):
pass
print(Parent.x, Child1.x, Child2.x)
print(id(Parent.x), id(Child1.x), id(Child2.x))
"""
运行结果:
1 1 1
140709706773328 140709706773328 140709706773328
"""
- 第二行(1 2 1):在
Child1.x = 2
中重写了子类Child1的变量 x ,该值仅仅在Child1类中被改变。显示id代码如下:
class Parent(object):
x = 1
class Child1(Parent):
pass
class Child2(Parent):
pass
print(Parent.x, Child1.x, Child2.x)
print(id(Parent.x), id(Child1.x), id(Child2.x))
Child1.x = 2
print(Parent.x, Child1.x, Child2.x)
print(id(Parent.x), id(Child1.x), id(Child2.x))
"""
运行结果:
1 1 1
140709706773328 140709706773328 140709706773328
1 2 1
140709706773328 140709706773360 140709706773328
"""
- 第三行(3 2 3):在
Parent.x = 3
中更改了父类Parent中变量 x 的值,这个改变只会影响所有未重写该变量的子类(例子中的Child2)。显示id代码如下:
class Parent(object):
x = 1
class Child1(Parent):
pass
class Child2(Parent):
pass
print(Parent.x, Child1.x, Child2.x)
print(id(Parent.x), id(Child1.x), id(Child2.x))
Child1.x = 2
print(Parent.x, Child1.x, Child2.x)
print(id(Parent.x), id(Child1.x), id(Child2.x))
Parent.x = 3
print(Parent.x, Child1.x, Child2.x)
print(id(Parent.x), id(Child1.x), id(Child2.x))
"""
运行结果:
1 1 1
140709706773328 140709706773328 140709706773328
1 2 1
140709706773328 140709706773360 140709706773328
3 2 3
140709706773392 140709706773360 140709706773392
"""
总结:在继承中,所有子类中的变量 x 原本都是引用父类中变量 x 的存储空间,但是Child1中的变量 x 单独指向了另一块存储空间。