Python:类属性的继承(列表)
从超类继承类属性,然后更改子类的值可以正常工作:
class Unit(object):
value = 10
class Archer(Unit):
pass
print Unit.value
print Archer.value
Archer.value = 5
print Unit.value
print Archer.value
导致输出:
10
10
10
5
这很好:弓箭手继承了单位的值,但是当我更改弓箭手的值时,单位的值保持不变。
现在,如果继承的值是一个列表,浅复制效果就会发生,超类的值也会受到影响:
class Unit(object):
listvalue = [10]
class Archer(Unit):
pass
print Unit.listvalue
print Archer.listvalue
Archer.listvalue[0] = 5
print Unit.listvalue
print Archer.listvalue
输出:
10
10
5
5
从超类继承列表时是否有办法“深复制”列表?
非常感谢
佐野
inheriting a class attribute from a super class and later changing the value for the subclass works fine:
class Unit(object):
value = 10
class Archer(Unit):
pass
print Unit.value
print Archer.value
Archer.value = 5
print Unit.value
print Archer.value
leads to the output:
10
10
10
5
which is just fine: Archer inherits the value from Unit, but when I change Archer's value, Unit's value remains untouched.
Now, if the inherited value is a list, the shallow copy effect strikes and the value of the superclass is also affected:
class Unit(object):
listvalue = [10]
class Archer(Unit):
pass
print Unit.listvalue
print Archer.listvalue
Archer.listvalue[0] = 5
print Unit.listvalue
print Archer.listvalue
Output:
10
10
5
5
Is there a way to "deep copy" a list when inheriting it from the super class?
Many thanks
Sano
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这不是浅拷贝或深拷贝的问题,而是引用和作业的问题。
第一种情况
Unit.value
和Archer.value
是引用相同值的两个变量。当您执行Archer.value = 5
时,您正在为 Acher.value 分配一个新引用。要解决您的问题,您需要为
Archer.list
分配一个新的列表值。如果这些值仅由类方法访问,那么最简单的解决方案是在类初始化时进行赋值。
It is not a matter of shallow or deep copies, it is a matter of references and assignments.
It the first case
Unit.value
andArcher.value
are two variables which reference the same value. When you doArcher.value = 5
, you are assigning a new reference to Acher.value.To solve your problem you need to assign a new list value to the
Archer.list
.If these values are only going to be accessed by class methods, then the simplest solution is to do the assignment when the class is initialized.
迈克尔的答案很好而且简单,但是如果您希望避免必须将该行添加到每个 Unit 子类中 - 也许您有一堆类似的其他列表,元类是解决问题
输出的一种简单方法:
您也可以扩展这个相同的想法来自动查找和复制 Unit 中定义的列表(和字典)
Michael's answer is nice and simple, but if you wish to avoid having to add that line to each Unit subclass - maybe you have a bunch of other lists like that one, a metaclass is an easy way to solve the problem
output:
You can also extend this same idea to automatically find and copy up lists (and dicts) defined in Unit
您可以复制 Archer 定义中的列表:
You coud copy the list in the definition of Archer: