Python类的继承问题

话不多说,直接上代码:

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 单独指向了另一块存储空间

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值