元组为什么可以包含可变项? [英] Why can tuples contain mutable items?

查看:65
本文介绍了元组为什么可以包含可变项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果元组是不可变的,那么为什么它可以包含可变项?

If a tuple is immutable then why can it contain mutable items?

当可变项(例如列表)确实被修改时,它所属的元组保持不变是一种矛盾.

It is seemingly a contradiction that when a mutable item such as a list does get modified, the tuple it belongs to maintains being immutable.

推荐答案

这是一个很好的问题.

关键的见解是,元组无法知道其中的对象是否可变.使对象可变的唯一方法是拥有更改其数据的方法.通常,无法检测到这一点.

The key insight is that tuples have no way of knowing whether the objects inside them are mutable. The only thing that makes an object mutable is to have a method that alters its data. In general, there is no way to detect this.

另一种见解是Python的容器实际上不包含任何东西.相反,它们保留对其他对象的引用.同样,Python的变量与编译语言中的变量不同.相反,变量名只是名称空间字典中的键,它们与对应的对象相关联. Ned Batchhelder在他的博客文章中对此进行了很好的解释.无论哪种方式,对象仅知道其引用计数.他们不知道这些引用是什么(变量,容器或Python内部函数).

Another insight is that Python's containers don't actually contain anything. Instead, they keep references to other objects. Likewise, Python's variables aren't like variables in compiled languages; instead the variable names are just keys in a namespace dictionary where they are associated with a corresponding object. Ned Batchhelder explains this nicely in his blog post. Either way, objects only know their reference count; they don't know what those references are (variables, containers, or the Python internals).

这两个见解共同解释了您的奥秘(为什么当基础列表更改时,包含"列表的不可变元组似乎也会更改).实际上,元组没有变化(它对其他对象的引用与以前相同).元组无法更改(因为它没有变异方法).当列表更改时,该元组不会收到有关更改的通知(该列表不知道它是由变量,元组还是其他列表引用的.)

Together, these two insights explain your mystery (why an immutable tuple "containing" a list seems to change when the underlying list changes). In fact, the tuple did not change (it still has the same references to other objects that it did before). The tuple could not change (because it did not have mutating methods). When the list changed, the tuple didn't get notified of the change (the list doesn't know whether it is referred to by a variable, a tuple, or another list).

虽然我们在讨论这个话题,但还有其他一些想法可以帮助您完善关于元组是什么,它们如何工作以及它们的预期用途的心理模型:

While we're on the topic, here are a few other thoughts to help complete your mental model of what tuples are, how they work, and their intended use:

  1. Tuples的特点是不变性强,而其目的则更多.
    元组是Python在一个屋檐下收集异构信息的一种方式.例如, s = ('www.python.org', 80) 将字符串和数字组合在一起,以便主机/端口对可以作为套接字(复合对象)传递.从这个角度来看,具有可变的组件是完全合理的.

  1. Tuples are characterized less by their immutability and more by their intended purpose.
    Tuples are Python's way of collecting heterogeneous pieces of information under one roof. For example, s = ('www.python.org', 80) brings together a string and a number so that the host/port pair can be passed around as a socket, a composite object. Viewed in that light, it is perfectly reasonable to have mutable components.

不可移植性与另一个属性可移植性紧密结合.但是哈希性不是绝对的属性.如果元组的组成部分之一不可哈希,则整个元组也不可哈希.例如,t = ('red', [10, 20, 30])不可散列.

Immutability goes hand-in-hand with another property, hashability. But hashability isn't an absolute property. If one of the tuple's components isn't hashable, then the overall tuple isn't hashable either. For example, t = ('red', [10, 20, 30]) isn't hashable.

最后一个示例显示了一个2元组,其中包含一个字符串和一个列表.元组本身是不可变的(即,它没有任何更改其内容的方法).同样,字符串是不可变的,因为字符串没有任何突变方法.列表对象确实具有变异方法,因此可以对其进行更改.这表明可变性是对象类型的属性-有些对象具有突变方法,而有些则没有.这并不会因为对象被嵌套而改变.

The last example shows a 2-tuple that contains a string and a list. The tuple itself isn't mutable (i.e. it doesn't have any methods that for changing its contents). Likewise, the string is immutable because strings don't have any mutating methods. The list object does have mutating methods, so it can be changed. This shows that mutability is a property of an object type -- some objects have mutating methods and some don't. This doesn't change just because the objects are nested.

记住两件事.首先,不变性不是魔术,而是缺少突变方法.其次,对象不知道哪些变量或容器引用了它们-他们只知道引用计数.

Remember two things. First, immutability is not magic -- it is merely the absence of mutating methods. Second, objects don't know what variables or containers refer to them -- they only know the reference count.

希望,这对您有用:-)

Hope, this was useful to you :-)

这篇关于元组为什么可以包含可变项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆