python中的None与NULL

None是一个对象,而NULL是一个类型。
Python中没有NULL,只有None,None有自己的特殊类型NoneType。
None不等于0、任何空字符串、False等。
在Python中,None、False、0、""(空字符串)、[](空列表)、()(空元组)、{}(空字典)都相当于False。

判断变量是否为空的高效方法是:
if X is None
if not X:当X为None、False、""、0、[]、()、{}时,not X为真,无法分辨
if not X is None:等价于if not (X is None)、if X is not None

判断空使用指南
if X is not None写法清晰明了,且不会出错,推荐使用;
if not x使用前,必须确定X为None、False、""、0、[]、()、{}时对判断无影响。

示例

    x = []
    y = None
    
    print 'X is None测试结果'
    print x is None #False
    print y is None #True
    
    print 'not X测试结果'
    print not x #True
    print not y #True
    
    print 'not X is None测试结果'
    print not x is None #True
    print not y is None #False
    
    print 'X is not None测试结果'
    print x is not None #True
    print y is not None #False
  • 12
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值