Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
522 views
in Technique[技术] by (71.8m points)

python - Python中的字符串比较:与== [重复](String comparison in Python: is vs. == [duplicate])

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 '' . (我注意到我正在编写的Python脚本表现得很松散,并将其追溯到一个无限循环,其中循环条件是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. (当我将其更改为!=''而不是is not '' ,它工作正常。)

Also, is it generally considered better to just use '==' by default, even when comparing int or Boolean values? (另外,即使比较int或Boolean值,通常还是最好还是默认使用'=='吗?) 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',因为我发现它在审美上更令人愉悦和pythonic(这就是我陷入这个陷阱的方式...),但是我想知道是否打算仅在您关心找到两个对象时才保留它?具有相同ID的对象。)

  ask by Coquelicot translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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也为True。)

Not always. (不总是。) NaN is a counterexample. (NaN是一个反例。) But usually , identity ( is ) implies equality ( == ). (但通常 ,身份( is )表示相等( == )。) The converse is not true: Two distinct objects can have the same value. (反之则不成立:两个不同的对象可以具有相同的值。)

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. (您可以使用==比较值,并在is比较的身份时。)

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. (有一个优化允许将小整数与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 . (为了与None比较, is None优于== 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',因为我发现它在审美上更令人愉悦和pythonic(这就是我陷入这个陷阱的方式...),但是我想知道是否打算仅在您关心找到两个对象时才保留它?具有相同ID的对象。)

Yes, that's exactly what it's for. (是的,这就是它的用途。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...