美文网首页
Python中的空值判断

Python中的空值判断

作者: MontyOak | 来源:发表于2017-04-15 10:53 被阅读14958次

    本文中所有代码均运行在Python 2.7上

    在实际的工作当中,我们难免要与空值打交道,相信不少初学者都会写出下面的代码:

    if a is None:
        do something.
    else:
        do the other thing.
    

    这样写看起来不错,但实际上会有问题。一般来讲,Python中会把下面几种情况当做空值来处理:

    • None
    • False
    • 0,0.0,0L
    • '',(),[],{}

    其中None的特殊之处在于,它既不是数值0,也不是某个数据结构的空值,它本身就是一个空值对象。它的类型是NoneType,它遵循单例模式,也就是说,在同一命名空间下的所有None其实质上都是同一个空值对象。

    >>> id(None)
    1795884240
    >>> None == 0
    False
    >>> None == ''
    False
    >>> a = None
    >>> id(a)
    1795884240
    >>> a == None
    True
    

    上面的判断显然不符合我们的期望:只有当a被显示赋值为None的情况下,a==None才为True

    那么,对于Python中更为广义的None值判断,我们应该怎么做呢?

    >>> a = ''    #这里仅以空字符串为例,其他空值同样适用
    >>> if a:
    ...        print 'a is not empty'
    ...    else:
    ...        print 'a is a empty string'
    'a is a empty string.' 
    

    可以看出,if a的判断方式得出了我们想要的结果,那么if a的判断方式究竟是一个怎样的过程呢?
    if a会首先去调用a的__nonzero__()去判断a是否为空,并返回True/False,若一个对象没有定义__nonzero__(),就去调用它的__len__()来进行判断(这里返回值为0代表空),若某一对象没有定义以上两种方法,则if a的结果永远为True
    接下来验证一下上面的说法:

    >>>class A(object):
    ...        def __nonzero__(self):
    ...            print 'running on the __nonzero__'
    ...            return True
    >>>class B(object):
    ...        def __len__(self):
    ...            print 'running on the __len__'
    ...            return False
    >>> a, b = A(), B()
    >>>if a:
    ...        print 'Yep'
    ...    else:
    ...        print 'Nop'
    running on the __nonzero__
    Yep
    >>>if b:
    ...        print 'Yep'
    ...    else:
    ...        print 'Nop'
    running on the __len__
    Nop
    

    相关文章

      网友评论

          本文标题:Python中的空值判断

          本文链接:https://www.haomeiwen.com/subject/tqjnattx.html