Why does “[] is [ ]” evaluate to False in python

前端 未结 4 838
無奈伤痛
無奈伤痛 2020-12-20 18:46

Try this in an interactive python shell.

[] is [ ]

The above returns False, why?

4条回答
  •  醉酒成梦
    2020-12-20 19:35

    In python is does a reference equality check like [] and [] they are different objects you can check that by

    print id([]),id([])
    

    or

     In [1]: id([])
    Out[1]: 140464629086976
    
    In [2]: id([])
    Out[2]: 140464628521656
    

    both will return different address and both are different object so is will always give false

    [] is []
    

    output

    false
    

提交回复
热议问题