Python'是'vs JavaScript === [英] Python 'is' vs JavaScript ===

查看:78
本文介绍了Python'是'vs JavaScript ===的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python使用'is'似乎与JavaScript'==='类似但不完全相似。

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

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

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

这里(对于JS)他们谈论相同且相同的类型。
http://www.w3schools.com/js/js_comparisons.asp

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

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

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 ===?

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

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

推荐答案

Python部件



Python Part


你可以有两个不同的实例(比方说)一串Bob和
使用'is'比较它们不会返回true吗?或者它实际上是
和===相同?

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 ===?



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

输出

Bob Bob
False True

注意:在大多数情况下Python实现,编译时间字符串被实习

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

另一个例子,

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

输出

True
False

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

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, is will return True for 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.

结论:

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

is will return True only 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, is will return True)

经验法则

永远不要使用 运算符检查两个对象是否具有相同的值。

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

你问题的其他部分是关于===运算符。让我们看看该运算符的工作原理。

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

引用ECMA 5.1规范,严格的等式比较算法就像这样定义

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



  1. 如果类型(x)与类型(y)不同,返回false。

  2. 如果Type(x)是Undefined,则返回true。

  3. If Type (x)为Null,返回true。

  4. 如果Type(x)为Number,则

  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. 如果x为NaN,返回false。

  2. 如果y是NaN,则返回false。

  3. 如果x与y的值相同,则返回true。

  4. 如果x是+ 0且y是-0,则返回true。

  5. 如果x是-0且y是+0,则返回true。

  6. 返回false。

  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.


  • 如果Type(x)是String,则如果x和y则返回true正是
    相同的字符序列(
    对应位置的长度和字符相同);否则,返回false。

  • 如果Type(x)是布尔值,如果x和y都为true或两者都是
    ,则返回true;否则返回false。否则,返回false。

  • 如果x和y引用同一个对象,则返回true。否则,返回
    false。




  • 最终结论



    我们可以比较Python的运算符和JavaScript的 === 运算符,因为Python的运算符只执行严格等式比较算法中的最后一项。

    Final Conclusion

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

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

    这篇关于Python'是'vs JavaScript ===的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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