How do I achieve the effect of the === operator in Python?

前端 未结 4 1295
轮回少年
轮回少年 2021-01-11 12:22

How do I achieve the effect of the === operator in Python?

For example, I don\'t want False == 0 to be True.

4条回答
  •  伪装坚强ぢ
    2021-01-11 13:01

    If you want to check that the value and type are the same use:

    x == y and type(x) == type(y)
    

    In Python, explicit type comparisons like this are usually avoided, but because booleans are a subclass of integers it's the only choice here.


    x is y compares identity—whether two names refer to the same object in memory. The Python boolean values are singletons so this will work when comparing them, but won't work for most types.

提交回复
热议问题