What is the difference between “ is None ” and “ ==None ”

后端 未结 5 919
无人共我
无人共我 2020-11-22 11:33

I recently came across this syntax, I am unaware of the difference.

I would appreciate it if someone could tell me the difference.

5条回答
  •  醉酒成梦
    2020-11-22 11:58

    In this case, they are the same. None is a singleton object (there only ever exists one None).

    is checks to see if the object is the same object, while == just checks if they are equivalent.

    For example:

    p = [1]
    q = [1]
    p is q # False because they are not the same actual object
    p == q # True because they are equivalent
    

    But since there is only one None, they will always be the same, and is will return True.

    p = None
    q = None
    p is q # True because they are both pointing to the same "None"
    

提交回复
热议问题