python中none与null有啥区别?
发布时间:2022-03-12 13:29:53 所属栏目:语言 来源:互联网
导读:python中none与null有啥区别?很多新手在python时,对于none与null容易混淆,因此,这篇文章就主要给大家讲讲none与null用法的不同,感兴趣的朋友就继续往下看吧,希望大家阅读完这篇文章能有所收获。 None是一个对象,而NULL是一个类型。 Python中没有NULL
|
python中none与null有啥区别?很多新手在python时,对于none与null容易混淆,因此,这篇文章就主要给大家讲讲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 补充:python中None与0、Null、false区别 None是Python中的一个关键字,None本身也是个一个数据类型,而这个数据类型就是None,它可0、空字符串以及false均不一样,这些都只是对象,而None也是一个类。 给个bool测试: val = None if val: print "None is true" else: print "None is not true" #输出 None is not true (编辑:张家口站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |

