Python 中的两个变量具有相同的 id,但不是列表或元组 [英] Two variables in Python have same id, but not lists or tuples

查看:29
本文介绍了Python 中的两个变量具有相同的 id,但不是列表或元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 中的两个变量具有相同的 id:

a = 10乙 = 10a 是 b>>>真的

如果我取两个list:

a = [1, 2, 3]b = [1, 2, 3]a 是 b>>>错误的

根据 此链接 Senderle 回答说,不可变对象引用具有相同的 id,而列表等可变对象具有不同的 id.

所以现在根据他的回答,元组应该具有相同的 id - 意思是:

a = (1, 2, 3)b = (1, 2, 3)a 是 b>>>错误的

理想情况下,由于元组不是可变的,它应该返回True,但它返回的是False

解释是什么?

解决方案

不可变对象没有相同的 id,事实上对于任何类型的对象都不是这样你分别定义.一般来说,每次在 Python 中定义一个对象时,都会创建一个具有新标识的新对象.然而,为了优化(大部分),对于小整数(-5 到 256 之间)和内部字符串有一些例外,具有特殊长度——通常小于 20 个字符——*是单例并具有相同的 id(实际上是一个具有多个指针的对象).你可以像下面这样检查:

<预><代码>>>>30 是 (20 + 10)真的>>>300 是 (200 + 100)错误的>>>'aa' * 2 是 'a' * 4真的>>>'aa' * 20 是 'a' * 40错误的

对于自定义对象:

<预><代码>>>>A类:... 经过...>>>A() is A() # 每次创建实例时,都会有一个具有新身份的新实例错误的

另请注意,is 运算符将检查对象的身份,而不是值.如果你想检查你应该使用的值 ==:

<预><代码>>>>300 == 3*100真的

而且由于元组或任何可变类型没有这样的优化或实习规则,如果你定义两个相同大小的元组,它们将获得自己的身份,因此不同的对象:

<预><代码>>>>a = (1,)>>>b = (1,)>>>>>>a 是 b错误的

还值得一提的是,单个整数"和内部字符串"的规则即使在迭代器中定义也是如此.

<预><代码>>>>a = (100, 700, 400)>>>>>>b = (100, 700, 400)>>>>>>a[0] 是 b[0]真的>>>a[1] 是 b[1]错误的

<小时>

<子>* 一篇很好的详细文章:http://guilload.com/python-string-interning/

Two variables in Python have the same id:

a = 10
b = 10
a is b
>>> True

If I take two lists:

a = [1, 2, 3]
b = [1, 2, 3]
a is b
>>> False

according to this link Senderle answered that immutable object references have the same id and mutable objects like lists have different ids.

So now according to his answer, tuples should have the same ids - meaning:

a = (1, 2, 3)
b = (1, 2, 3)
a is b
>>> False

Ideally, as tuples are not mutable, it should return True, but it is returning False!

What is the explanation?

解决方案

Immutable objects don't have the same id, and as a mater of fact this is not true for any type of objects that you define separately. Generally speaking, every time you define an object in Python, you'll create a new object with a new identity. However, for the sake of optimization (mostly) there are some exceptions for small integers (between -5 and 256) and interned strings, with a special length --usually less than 20 characters--* which are singletons and have the same id (actually one object with multiple pointers). You can check this like following:

>>> 30 is (20 + 10)
True
>>> 300 is (200 + 100)
False
>>> 'aa' * 2 is 'a' * 4
True
>>> 'aa' * 20 is 'a' * 40
False

And for a custom object:

>>> class A:
...    pass
... 
>>> A() is A() # Every time you create an instance you'll have a new instance with new identity
False

Also note that the is operator will check the object's identity, not the value. If you want to check the value you should use ==:

>>> 300 == 3*100
True

And since there is no such optimizational or interning rule for tuples or any mutable type for that matter, if you define two same tuples in any size they'll get their own identities, hence different objects:

>>> a = (1,)
>>> b = (1,)
>>>
>>> a is b
False

It's also worth mentioning that rules of "singleton integers" and "interned strings" are true even when they've been defined within an iterator.

>>> a = (100, 700, 400)
>>>
>>> b = (100, 700, 400)
>>>
>>> a[0] is b[0]
True
>>> a[1] is b[1]
False


* A good and detailed article on this: http://guilload.com/python-string-interning/

这篇关于Python 中的两个变量具有相同的 id,但不是列表或元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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