How to create the int 1 at two different memory locations?

后端 未结 3 750
不思量自难忘°
不思量自难忘° 2020-12-10 19:47

I want to show someone how using is instead of == to compare integers can fail. I thought this would work, but it didn\'t:

>>         


        
3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-10 20:24

    You can do:

    >>> 0 - 6 is -6
    False
    >>> 0 - 6 == -6
    True
    

    It also works for bigger numbers:

    >>> 1000 + 1 is 1001
    False
    >>> 1000 + 1 == 1001
    True
    

    It depends on what you want to demonstrate but the above highlights the difference in functionality between is and ==.

提交回复
热议问题