【问题标题】:Why don't these statements both return 'true'? [duplicate]为什么这些语句都不返回“真”? [复制]
【发布时间】:2016-02-20 08:28:07
【问题描述】:

所以我在 python 中遇到了一些奇怪的行为:

>>> 2+1 is 3
True
>>> 2000+1 is 2001
False

使用大整数时逻辑不正确,这是为什么呢?

【问题讨论】:

    标签: python math logic theory


    【解决方案1】:

    如果两个变量指向同一个对象,is 将返回 True。这样就有id

    In [21]: id(3)
    Out[21]: 15538056
    
    In [22]: id(2+1)
    Out[22]: 15538056
    
    In [23]: id(2001), id(2000+1)
    Out[23]: (52399576, 54526360)
    

    【讨论】:

      猜你喜欢
      • 2013-03-20
      • 2011-12-18
      • 2015-05-06
      • 1970-01-01
      • 2013-11-29
      • 2022-11-25
      • 2017-05-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多