Where is the NoneType located in Python 3.x?

前端 未结 3 1633
孤城傲影
孤城傲影 2020-12-17 08:32

In Python 3, I would like to check whether value is either string or None.

One way to do this is

assert type(value) in { st         


        
3条回答
  •  既然无缘
    2020-12-17 09:05

    Please use type(None). You can use python shell to check like in the below function in which I use type(None) in order to change from None to NoneType.

    def to_unicode(value):
    '''change value to unicode'''
    try:
        if isinstance(value, (str,type(None))):
            return value
        if not isinstance(value, bytes):
            raise TypeError("Expected bytes, unicode, or None; got %r" % type(value))
        return value.decode("utf-8")
    except UnicodeDecodeError:
        return repr(value)
    

提交回复
热议问题