None Python error/bug?

前端 未结 4 2040
忘了有多久
忘了有多久 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:43

    None is the just a value of types.NoneType, it's not a type.

    And the error is clear enough:

    TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and types

    From the docs:

    None is the sole value of types.NoneType. None is frequently used to represent the absence of a value, as when default arguments are not passed to a function.

    You can use types.NoneType

    >>> from types import NoneType
    >>> isinstance(None, NoneType)
    True
    

    is operator also works fine:

    >>> a = None
    >>> a is None
    True
    

提交回复
热议问题