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

后端 未结 5 810
南方客
南方客 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 16:01

    For more understanding why this occurs take a look to Python-2.6.5/Objects/intobject.c:78:small_ints array and Python-2.6.5/Objects/intobject.c:1292:_PyInt_Init function in python sources.

    Also similar thing occurs with lists:

    
    >>> a = [12]
    >>> id_a = id(a)
    >>> del(a)
    >>> id([1,2,34]) == id_a
    True
    >>> 
    

    Removed lists are not destroyed. They are reused

提交回复
热议问题