乱搞别名

发布于 2024-10-21 04:51:08 字数 393 浏览 1 评论 0原文

我有以下代码,可以按预期工作:

a = [1, 2, 3, 4]
b = a

>>> b is a
True

如果我稍微改变一下它仍然可以工作:

a = [1, 2, 3, 4]
b = a[2] * 2

>>> b is a[2] * 2
True

现在的问题是:

a = [1, 2, 3, 4]
b = a * 2

>>> b is a * 2
False    

有人可以向我解释一下为什么它返回 False,而 b 是 a[2] * 2 返回 True?

I have the following code that works as expected:

a = [1, 2, 3, 4]
b = a

>>> b is a
True

if I change it up a little it still works:

a = [1, 2, 3, 4]
b = a[2] * 2

>>> b is a[2] * 2
True

Now the problem:

a = [1, 2, 3, 4]
b = a * 2

>>> b is a * 2
False    

Can someone please explain to me why this returns False, while b is a[2] * 2 returns True?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蹲墙角沉默 2024-10-28 04:51:08

a 是列表时,a * 2 创建一个新的列表实例。如果您调用 a * 2 两次,则会创建两个新的列表实例 - 这就是为什么 b is a * 2 会产生 False

b is a[2] * 2 得到 True 的原因是 CPython 对缓存小整数的优化。由于整数在 Python 中是不可变的,因此实际上获取新实例还是缓存实例并不重要,并且对于小整数,如果再次命中相同的整数,Python 将返回缓存版本。也尝试一下

>>> a = [1, 2, 300, 4]
>>> b = a[2] * 2
>>> b is a[2] * 2
False

When a is a list, a * 2 creates a new list instance. If you call a * 2 twice, you create two new list instances -- that's why b is a * 2 yields False.

The reason that you get True for b is a[2] * 2 is an optimisation of CPython that caches small integers. Since integers are immutable in Python, it actually does not matter if you get a new instance or a cached one, and for small integers, Python returns a cached version if you hit the same integer again. Also try

>>> a = [1, 2, 300, 4]
>>> b = a[2] * 2
>>> b is a[2] * 2
False
开始看清了 2024-10-28 04:51:08

比较时,相同的列表并不等效,除非它们引用相同的列表。仅仅因为列表具有相同的值并不意味着它们引用内存中的相同列表。

例如,

>>> a = [1,2,3]
>>> id(a) # memory location list a references
161267628
>>> b = [1,2,3]
>>> id(b) # memory location list b references, a different memory location than list a
161276396
>>> a == b 
True
>>> a is b
False
>>> c = a # by this method of assignment; c points to the same point that a does;
# hence changes to a and changes to c, will change both.
>>> id(c) # memory location that c references; this is the same as where a points.
161267628
>>> a is c
True
>>> c.append(4)
>>> print a
[1, 2, 3, 4]
>>> print b
[1, 2, 3]
>>> print c
[1, 2, 3, 4]
>>> d = a[:] # this just copies the values in a to d.  
>>> id(d)
161277036

它们指向不同的内存位置是有道理的,因为如果您可能想说更改第一个列表(例如将 4 附加到末尾 a)而不修改 <如果 ab 指向内存中的同一位置,则这是不可能的。

Identical lists aren't equivalent when compared is unless they reference the same list. Just because the lists have the same values doesn't mean they reference the same list in memory.

E.g.,

>>> a = [1,2,3]
>>> id(a) # memory location list a references
161267628
>>> b = [1,2,3]
>>> id(b) # memory location list b references, a different memory location than list a
161276396
>>> a == b 
True
>>> a is b
False
>>> c = a # by this method of assignment; c points to the same point that a does;
# hence changes to a and changes to c, will change both.
>>> id(c) # memory location that c references; this is the same as where a points.
161267628
>>> a is c
True
>>> c.append(4)
>>> print a
[1, 2, 3, 4]
>>> print b
[1, 2, 3]
>>> print c
[1, 2, 3, 4]
>>> d = a[:] # this just copies the values in a to d.  
>>> id(d)
161277036

This makes sense that they point to different memory locations, because if you may want to say change the first list (like append 4 to the end a) without modifying b which wouldn't be possible if a and b pointed to the same location in memory.

獨角戲 2024-10-28 04:51:08

a * 2 构造一个新列表,而整数与其自身具有同一性。

>>> type(a[2] * 2)
<type 'int'>
>>> type(a * 2)
<type 'list'>

a * 2 constructs a new list, while an integer has identity with itself.

>>> type(a[2] * 2)
<type 'int'>
>>> type(a * 2)
<type 'list'>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文