Python中的字符串比较:is vs = [duplicate]

时间:2021-06-15 23:00:28

This question already has an answer here:

这个问题已经有了答案:

I noticed a Python script I was writing was acting squirrelly, and traced it to an infinite loop, where the loop condition was while line is not ''. Running through it in the debugger, it turned out that line was in fact ''. When I changed it to !='' rather than is not '', it worked fine.

我注意到我正在编写的一个Python脚本行为古怪,并将它跟踪到一个无限循环,其中循环条件是,而行不是”。在调试器中运行它,结果发现这一行实际上是。当我把它改为!=“而不是不”时,它运行得很好。

Also, is it generally considered better to just use '==' by default, even when comparing int or Boolean values? I've always liked to use 'is' because I find it more aesthetically pleasing and pythonic (which is how I fell into this trap...), but I wonder if it's intended to just be reserved for when you care about finding two objects with the same id.

此外,即使在比较int或Boolean值时,默认使用'= '是否更可取?我一直喜欢使用“is”,因为我觉得它更美观,更符合python语言(这也是我陷入这个陷阱的原因…),但我不知道它是否只是为了在你想要找到两个具有相同id的对象时才使用。

4 个解决方案

#1


527  

For all built-in Python objects (like strings, lists, dicts, functions, etc.), if x is y, then x==y is also True.

对于所有内置的Python对象(如字符串、列表、命令、函数等),如果x是y,那么x= y也是正确的。

Not always. NaN is a counterexample. But usually, identity (is) implies equality (==). The converse is not true: Two distinct objects can have the same value.

不总是正确的。南是一个反例。但是,身份(is)通常意味着平等(=)。反之则不成立:两个不同的对象可以具有相同的值。

Also, is it generally considered better to just use '==' by default, even when comparing int or Boolean values?

此外,即使在比较int或Boolean值时,默认使用'= '是否更可取?

You use == when comparing values and is when comparing identities.

比较值时使用==,比较恒等式时使用==。

When comparing ints (or immutable types in general), you pretty much always want the former. There's an optimization that allows small integers to be compared with is, but don't rely on it.

当比较int(或一般的不可变类型)时,您几乎总是希望使用前者。有一种优化方法可以将小整数与is进行比较,但是不要依赖它。

For boolean values, you shouldn't be doing comparisons at all. Instead of:

对于布尔值,根本不应该进行比较。而不是:

if x == True:
    # do something

write:

写:

if x:
    # do something

For comparing against None, is None is preferred over == None.

与无相比较,无为优选。

I've always liked to use 'is' because I find it more aesthetically pleasing and pythonic (which is how I fell into this trap...), but I wonder if it's intended to just be reserved for when you care about finding two objects with the same id.

我一直喜欢使用“is”,因为我觉得它更美观,更符合python语言(这也是我陷入这个陷阱的原因…),但我不知道它是否只是为了在你想要找到两个具有相同id的对象时才使用。

Yes, that's exactly what it's for.

是的,这正是它的作用。

#2


231  

I would like to show a little example on how is and == are involved in immutable types. Try that:

我想展示一个关于is和==如何涉及到不可变类型的示例。试一试:

a = 19998989890
b = 19998989889 +1
>>> a is b
False
>>> a == b
True

is compares two objects in memory, == compares their values. For example, you can see that small integers are cached by Python:

是比较内存中的两个对象,=比较它们的值。例如,您可以看到小整数被Python缓存:

c = 1
b = 1
>>> b is c
True

You should use == when comparing values and is when comparing identities. (Also, from an English point of view, "equals" is different from "is".)

比较值时应该使用==,比较身份时应该使用=。(同样,从英语的角度来看,“equals”与“is”是不同的。)

#3


61  

The logic is not flawed. The statement

这种逻辑没有缺陷。该声明

if x is y then x==y is also True

如果x= y,那么x= y也成立

should never be read to mean

永远不应该被解读为意味着什么

if x==y then x is y

如果x= y,那么x= y

It is a logical error on the part of the reader to assume that the converse of a logic statement is true. See http://en.wikipedia.org/wiki/Converse_(logic)

假设逻辑语句的反义词是true,这是读者的逻辑错误。看到http://en.wikipedia.org/wiki/Converse_(逻辑)

#4


25  

See This question

看到这个问题

Your logic in reading

你的逻辑在阅读

For all built-in Python objects (like strings, lists, dicts, functions, etc.), if x is y, then x==y is also True.

对于所有内置的Python对象(如字符串、列表、命令、函数等),如果x是y,那么x= y也是正确的。

is slightly flawed.

略是有缺陷的。

If is applies then == will be True, but it does NOT apply in reverse. == may yield True while is yields False.

如果被应用,那么==将是正确的,但它并不适用于相反的情况。= may yield True,而is yield False。

#1


527  

For all built-in Python objects (like strings, lists, dicts, functions, etc.), if x is y, then x==y is also True.

对于所有内置的Python对象(如字符串、列表、命令、函数等),如果x是y,那么x= y也是正确的。

Not always. NaN is a counterexample. But usually, identity (is) implies equality (==). The converse is not true: Two distinct objects can have the same value.

不总是正确的。南是一个反例。但是,身份(is)通常意味着平等(=)。反之则不成立:两个不同的对象可以具有相同的值。

Also, is it generally considered better to just use '==' by default, even when comparing int or Boolean values?

此外,即使在比较int或Boolean值时,默认使用'= '是否更可取?

You use == when comparing values and is when comparing identities.

比较值时使用==,比较恒等式时使用==。

When comparing ints (or immutable types in general), you pretty much always want the former. There's an optimization that allows small integers to be compared with is, but don't rely on it.

当比较int(或一般的不可变类型)时,您几乎总是希望使用前者。有一种优化方法可以将小整数与is进行比较,但是不要依赖它。

For boolean values, you shouldn't be doing comparisons at all. Instead of:

对于布尔值,根本不应该进行比较。而不是:

if x == True:
    # do something

write:

写:

if x:
    # do something

For comparing against None, is None is preferred over == None.

与无相比较,无为优选。

I've always liked to use 'is' because I find it more aesthetically pleasing and pythonic (which is how I fell into this trap...), but I wonder if it's intended to just be reserved for when you care about finding two objects with the same id.

我一直喜欢使用“is”,因为我觉得它更美观,更符合python语言(这也是我陷入这个陷阱的原因…),但我不知道它是否只是为了在你想要找到两个具有相同id的对象时才使用。

Yes, that's exactly what it's for.

是的,这正是它的作用。

#2


231  

I would like to show a little example on how is and == are involved in immutable types. Try that:

我想展示一个关于is和==如何涉及到不可变类型的示例。试一试:

a = 19998989890
b = 19998989889 +1
>>> a is b
False
>>> a == b
True

is compares two objects in memory, == compares their values. For example, you can see that small integers are cached by Python:

是比较内存中的两个对象,=比较它们的值。例如,您可以看到小整数被Python缓存:

c = 1
b = 1
>>> b is c
True

You should use == when comparing values and is when comparing identities. (Also, from an English point of view, "equals" is different from "is".)

比较值时应该使用==,比较身份时应该使用=。(同样,从英语的角度来看,“equals”与“is”是不同的。)

#3


61  

The logic is not flawed. The statement

这种逻辑没有缺陷。该声明

if x is y then x==y is also True

如果x= y,那么x= y也成立

should never be read to mean

永远不应该被解读为意味着什么

if x==y then x is y

如果x= y,那么x= y

It is a logical error on the part of the reader to assume that the converse of a logic statement is true. See http://en.wikipedia.org/wiki/Converse_(logic)

假设逻辑语句的反义词是true,这是读者的逻辑错误。看到http://en.wikipedia.org/wiki/Converse_(逻辑)

#4


25  

See This question

看到这个问题

Your logic in reading

你的逻辑在阅读

For all built-in Python objects (like strings, lists, dicts, functions, etc.), if x is y, then x==y is also True.

对于所有内置的Python对象(如字符串、列表、命令、函数等),如果x是y,那么x= y也是正确的。

is slightly flawed.

略是有缺陷的。

If is applies then == will be True, but it does NOT apply in reverse. == may yield True while is yields False.

如果被应用,那么==将是正确的,但它并不适用于相反的情况。= may yield True,而is yield False。