“是"的类型关键字可能等效于Python中的等于运算符 [英] Types for which "is" keyword may be equivalent to equality operator in Python

查看:102
本文介绍了“是"的类型关键字可能等效于Python中的等于运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Python中的某些类型,is运算符似乎等同于==运算符.例如:

For some types in Python, the is operator seems to be equivalent to the == operator. For example:

>>> 1 is 1
True
>>> "a spoon" is "a spoon"
True
>>> (1 == 1) is (2 == 2)
True

但是,并非总是如此:

>>> [] == []
True
>>> [] is []
False

这对于可变类型(例如列表)有意义.但是,诸如元组之类的不可变类型似乎表现出相同的行为:

This makes sense for mutable types such as lists. However, immutable types such as tuples seem to display the same behavior:

>>> (1, 2) == (1, 2)
True
>>> (1, 2) is (1, 2)
False

这引起了几个问题:

  1. ==/is的等效性是否与不变性有关?
  2. 是否指定了上述行为或实现细节?
  3. 最重要(基本上),我怎么知道分配是导致对象复制还是对其进行引用?
  1. Is the == / is equivalence related to immutability?
  2. Are the behaviors above specified, or an implementation detail?
  3. Most importantly (and basically), how can I know if an assignment will result in a copy of an object being made, or a reference to it being made?

更新: 如果总是通过引用进行分配,那么为什么不打印以下内容2?

Update: If assignment is always by reference, why doesn't the following print 2?:

>>> a = 1
>>> b = a
>>> a = 2
>>> b
1

为什么这不等效于以下C代码段:

Why isn't this equivalent to the following C snippet:

int a = 1;
int *b = &a;
a = 2;
printf("%d\n", *b);

对此问题的新颖性表示歉意,但是我是Python新手,因此认为了解这一点很重要.您会建议阅读任何有关理解这类问题的文章吗?

Apologies for the newbness of this question, but I am a Python novice and feel that it is important to understand this. Is there any reading you would recommend to understand these sort of issues?

推荐答案

is运算符测试两个对象在物理上是否相同,这意味着它们在内存中是否具有相同的地址.也可以使用id()函数进行测试:

The is operator tests if two objects are physically the same, that means if they have the same address in memory. This can also be tested using the id() function:

>>> a = 1
>>> b = 1
>>> a is b
True
>>> id(a) == id(b)
True

==运算符则测试语义相等性.也可以通过实现__eq__()函数由自定义类覆盖.从语义上说,两个不同的列表如果它们的元素都相等,则相等,但实际上它们将是不同的对象.

The == operator on the other hand, tests for semantical equality. This can also be overridden by custom classes by implementing the __eq__() function. Semantically, two different lists are equal if their elements are all equal, but physically they will be different objects.

不可实现的类型(例如字符串和元组)可能会被Python实现池合并,因此两个文字字符串对象实际上在物理上是相同的.但这并不意味着您可以始终使用is来比较这些类型,如以下示例所示:

Immutable types like strings and tuples might be pooled by the Python implementation, so that two literal string objects are in fact physically identical. But this does not mean that you can always use is to compare those types, as demonstrated in the following example:

>>> "foobar" is "foobar"   # The interpreter knows that the string literals are
True                       # equal and creates only one shared object.
>>> a = "foobar"
>>> b = "foobar"
>>> a is b        # "foobar" comes from the pool, so it is still the same object.
True
>>> b = "foo"     # Here, we construct another string "foobar" dynamically that is
>>> b += "bar"    # physically not the same as the pooled "foobar".
>>> a == b
True
>>> a is b
False

Python中的赋值始终将对对象的引用绑定到变量名,并且从不暗示任何副本.

Assignments in Python always bind the reference to an object to a variable name and never implies a copy.

更新

类似于C,想想Python变量始终是指针:

Analogous to C, think of Python variables always being pointers:

>>> a = 1
>>> b = a
>>> a = 2
>>> b
1

大致等同于:

const int ONE = 1;
const int TWO = 2;

int *a = &ONE;
int *b = a;  /* b points to 1 */
a = &TWO;    /* a points to 2, b still points to 1 */

这篇关于“是"的类型关键字可能等效于Python中的等于运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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