python中的None与NULL用法说明
作者:小小的刀 时间:2022-01-01 13:59:15
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
来源:https://blog.csdn.net/u012596210/article/details/93735051
标签:python,None,NULL
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
pandas行和列的获取的实现
2022-06-05 07:57:49
详解PyTorch预定义数据集类datasets.ImageFolder使用方法
2022-01-30 20:15:07
asp随机产生注册用户密码
2007-10-17 12:33:00
javascript面向对象技术基础(三)
2010-02-07 13:11:00
asp如何终止浏览器的 CAHCE 页面?
2010-07-07 12:25:00
python多线程http下载实现示例
2023-12-03 00:15:34
详解Python并发编程之创建多线程的几种方法
2023-03-31 19:34:37
python脚本爬取字体文件的实现方法
2022-09-07 18:20:55
Python使用future处理并发问题方案详解
2022-12-10 18:16:53
php设计模式之装饰模式应用案例详解
2023-11-14 13:37:20
Python的matplotlib绘图如何修改背景颜色的实现
2023-10-05 08:12:43
![](https://img.aspxhome.com/file/2023/0/62900_0s.png)
python网络编程学习笔记(四):域名系统
2021-07-15 10:16:49
PHP实现的curl批量请求操作示例
2023-11-17 01:51:10
数组保存为txt, npy, csv 文件, 数组遍历enumerate的方法
2021-05-05 05:36:39
![](https://img.aspxhome.com/file/2023/7/87597_0s.jpg)
讲解SQL Server数据库触发器的安全隐患
2009-02-24 17:46:00
XML与HTML的结合(下)
2008-09-05 17:20:00
轻设计,让网站灵敏轻便的6个技巧
2009-12-07 21:26:00
![](https://img.aspxhome.com/file/UploadPic/200912/7/logo2-92s.gif)
Python使用pandas对数据进行差分运算的方法
2021-09-28 06:56:07
Golang并发编程之调度器初始化详解
2023-07-13 08:47:11
![](https://img.aspxhome.com/file/2023/5/99635_0s.png)
python paramiko实现ssh远程访问的方法
2021-07-17 23:03:55