Python“是”与 JavaScript ===

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21774629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 21:39:38  来源:igfitidea点击:

Python 'is' vs JavaScript ===

javascriptpython

提问by R Claven

The Python use of 'is' seems to be similar to JavaScript '===' but not quite.

Python 中“is”的使用似乎与 JavaScript 的“===”类似,但并不完全相同。

Here they talk about exact instances: http://www.learnpython.org/en/Conditions

他们在这里谈论确切的实例:http: //www.learnpython.org/en/Conditions

here (for JS) they talk about "equal AND the same type." http://www.w3schools.com/js/js_comparisons.asp

在这里(对于 JS)他们谈论“相等和相同类型”。 http://www.w3schools.com/js/js_comparisons.asp

SO can you have two different instances of (say) a string of "Bob" and have them not return true when compared using 'is'? Or is it infact the same as ===?

所以你可以有两个不同的实例(比如)一串“Bob”,并且在使用“is”进行比较时让它们不返回true?或者它实际上与 === 相同?

I am guessing this is related to strict vs non-strict typed languages. . . .

我猜这与严格与非严格类型语言有关。. . .

回答by thefourtheye

Python Part

蟒蛇部分

SO can you have two different instances of (say) a string of "Bob" and have them not return true when compared using 'is'? Or is it infact the same as ===?

所以你可以有两个不同的实例(比如)一串“Bob”,并且在使用“is”进行比较时让它们不返回true?或者它实际上与 === 相同?

a = "Bob"
b = "{}".format("Bob")
print a, b
print a is b, a == b

Output

输出

Bob Bob
False True

Note:In most of the Python implementations, compile time Strings are interned.

注意:在大多数 Python 实现中,编译时字符串是 interned

Another example,

另一个例子,

print 3 is 2+1
print 300 is 200+100

Output

输出

True
False

This is because, small ints (-5 to 256) in Python are cached internally. So, whenever they are used in the programs, the cached integers are used. So, iswill return Truefor them. But if we choose bigger numbers, like in the second example, (300 is 200+100) it is not True, because they are NOT cached.

这是因为 Python 中的小整数(-5 到 256)是在内部缓存的。因此,无论何时在程序中使用它们,都会使用缓存的整数。所以,is会回来True找他们。但是如果我们选择更大的数字,就像在第二个例子中那样, ( 300 is 200+100) 它不是真的,因为它们没有被缓存。

Conclusion:

结论:

iswill return Trueonly when the objects being compared are the same object, which means they point to the same location in memory. (It solely depends on the python implementation to cache/intern objects. In that case, iswill return True)

isTrue只有当被比较的对象是同一个对象时才会返回,这意味着它们指向内存中的相同位置。(它完全取决于缓存/实习对象的python实现。在这种情况下,is将返回True

Rule of thumb:

经验法则:

NEVER use isoperator to check if two objects have the same value.

切勿使用is运算符检查两个对象是否具有相同的值。



JavaScript Part

JavaScript 部分

Other part of your question is about === operator. Lets see how that operator works.

您问题的其他部分是关于 === 运算符。让我们看看该运算符是如何工作的。

Quoting from ECMA 5.1 Specs, The Strict Equality Comparison Algorithmis defined like this

引用 ECMA 5.1 规范,严格相等比较算法是这样定义的

  1. If Type(x) is different from Type(y), return false.
  2. If Type(x) is Undefined, return true.
  3. If Type(x) is Null, return true.
  4. If Type(x) is Number, then
    1. If x is NaN, return false.
    2. If y is NaN, return false.
    3. If x is the same Number value as y, return true.
    4. If x is +0 and y is ?0, return true.
    5. If x is ?0 and y is +0, return true.
    6. Return false.
  5. If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions); otherwise, return false.
  6. If Type(x) is Boolean, return true if x and y are both true or both false; otherwise, return false.
  7. Return true if x and y refer to the same object. Otherwise, return false.
  1. 如果 Type(x) 与 Type(y) 不同,则返回 false。
  2. 如果 Type(x) 未定义,则返回 true。
  3. 如果 Type(x) 为 Null,则返回 true。
  4. 如果 Type(x) 是 Number,则
    1. 如果 x 是 NaN,则返回 false。
    2. 如果 y 是 NaN,则返回 false。
    3. 如果 x 与 y 的 Number 值相同,则返回 true。
    4. 如果 x 是 +0 并且 y 是 ?0,则返回 true。
    5. 如果 x 是 ?0 并且 y 是 +0,则返回 true。
    6. 返回假。
  5. 如果 Type(x) 是 String,那么如果 x 和 y 是完全相同的字符序列(相同的长度和相应位置的相同字符),则返回 true;否则,返回false。
  6. 如果 Type(x) 是布尔值,如果 x 和 y 都为真或都为假,则返回真;否则,返回false。
  7. 如果 x 和 y 引用同一个对象,则返回 true。否则,返回false。

Final Conclusion

定论

We can NOTcompare Python's isoperator and JavaScript's ===operator, because Python's isoperator does only the last item in the Strict Equality Comparison Algorithm.

我们可以比较Python的is运营商和JavaScript的===运营商,因为Python的is运营商确实只有在完全平等比较算法的最后一个项目。

7. Return true if x and y refer to the same object. Otherwise, return false.

回答by Sean Vieira

Python's iskeyword compares references (and so is about identity) while ===does a minimal amount of coercion(and is therefore concerned with equality, at least in the case of primitives) so they are different.

Python 的is关键字比较引用(因此是关于身份的),同时===进行最少数量的强制(因此关注相等性,至少在原语的情况下),因此它们是不同的。

As I understand it, things that are concerned with identity are concerned with uniqueness from the runtime's point of view (do these two variables point to the same address in memory) while equality is concerned with the uniqueness of the contentsof the variables (are these two variables equivalent, regardless of where they are placed in memory relative to each other).

据我了解,从运行时的角度来看,与身份有关的事情与唯一性有关(这两个变量是否指向内存中的同一地址),而相等则与变量内容的唯一性有关(这些是两个变量是等价的,无论它们在内存中相对于彼此的位置如何)。

回答by Ignacio Vazquez-Abrams

Completely different.

完全不同。

>>> a = 'foo'
>>> b = 'bar'
>>> a + b is 'foobar'
False
>>> 1000 + 1 is 1001
False

回答by Bakuriu

>>> a = "Hello, World!!!"
>>> b = "Hello, World!!!"
>>> a is b
False

However note that:

但请注意:

>>> a = "Bob"
>>> b = "Bob"
>>> a is b
True

In this case it condition was Truebecause the compiler is free to intern string literals, and thus reuse the same object, and it does do that with small strings. However there is no guarantee as to when this happens of if this happens at all and the behaviour changes between versions and implementations.

在这种情况下,它的条件是True因为编译器可以自由地实习字符串文字,从而重用相同的对象,并且它确实使用小字符串来做到这一点。但是,无法保证何时会发生这种情况,或者是否会发生这种情况,并且版本和实现之间的行为会发生变化。



A realiable Falseoutput should be:

一个切实可行的False输出应该是:

>>> a = 'Hello, World!!!!'[:-1]
>>> b = 'Hello, World!!!!'[:-1]
>>> a is b
False

Or anything that actually computesthe strings.

或者任何实际计算字符串的东西。