Strange Python behavior from inappropriate usage of 'is not' comparison?

后端 未结 5 808
南方客
南方客 2021-01-22 15:12

I (incorrectly?) used \'is not\' in a comparison and found this curious behavior:

>>> a = 256
>>> b = int(\'256\')
>>> c = 300
>>         


        
5条回答
  •  鱼传尺愫
    2021-01-22 15:56

    "is" is not a check of equality of value, but a check that two variables point to the same instance of an object.

    ints and strings are confusing for this as is and == can happen to give the same result due to how the internals of the language work.

提交回复
热议问题