None Python error/bug?

前端 未结 4 2041
忘了有多久
忘了有多久 2021-02-05 07:33

In Python you have the None singleton, which acts pretty oddly in certain circumstances:

>>> a = None
>>> type(a)


        
4条回答
  •  既然无缘
    2021-02-05 07:51

    None is not a type, it is the singleton instance itself - and the second argument of isinstance must be a type, class or tuple of them. Hence, you need to use NoneType from types.

    from types import NoneType
    print isinstance(None, NoneType)
    print isinstance(None, (NoneType, str, float))
    
    True
    True
    

    Although, I would often be inclined to replace isinstance(x, (NoneType, str, float)) with x is None or isinstance(x, (str, float)).

提交回复
热议问题