python代码`if not x:` 和`if x is not None:`和`if not x is None:`使用
来源:互联网 发布:保益读屏软件苹果下载 编辑:程序博客网 时间:2024/05/21 13:57
代码中经常会有变量是否为None的判断,有三种主要的写法:
第一种是`if x is None`;
第二种是 `if not x:`;
第三种是`if not x is None`(这句这样理解更清晰`if not (x is None)`) 。
如果你觉得这样写没啥区别,那么你可就要小心了,这里面有一个坑。先来看一下代码:
- >>> x = 1
- >>> not x
- False
- >>> x = [1]
- >>> not x
- False
- >>> x = 0
- >>> not x
- True
- >>> x = [0] # You don’t want to fall in this one.
- >>> not x
- False
>>> x = 1>>> not xFalse>>> x = [1]>>> not xFalse>>> x = 0>>> not xTrue>>> x = [0] # You don't want to fall in this one.>>> not xFalse
在Python中 None, False, 空字符串”“, 0, 空列表[], 空字典{}, 空元组()都相当于False ,即:
- <strong>not None == not False == not ” == not 0 == not [] == not {} == not ()</strong>
not None == not False == not '' == not 0 == not [] == not {} == not ()
因此在使用列表的时候,如果你想区分x==[]和x==None两种情况的话, 此时`if not x:`将会出现问题:
- >>> x = []
- >>> y = None
- >>>
- >>> x is None
- False
- >>> y is None
- True
- >>>
- >>>
- >>> not x
- True
- >>> not y
- True
- >>>
- >>>
- >>> not x is None
- >>> True
- >>> not y is None
- False
- >>>
>>> x = []>>> y = None>>> >>> x is NoneFalse>>> y is NoneTrue>>> >>> >>> not xTrue>>> not yTrue>>> >>> >>> not x is None>>> True>>> not y is NoneFalse>>>也许你是想判断x是否为None,但是却把`x==[]`的情况也判断进来了,此种情况下将无法区分。
对于习惯于使用if not x这种写法的pythoner,必须清楚x等于None, False, 空字符串”“, 0, 空列表[], 空字典{}, 空元组()时对你的判断没有影响才行。
而对于`if x is not None`和`if not x is None`写法,很明显前者更清晰,而后者有可能使读者误解为`if (not x) is None`,因此推荐前者,同时这也是谷歌推荐的风格
结论:
`if x is not None`是最好的写法,清晰,不会出现错误,以后坚持使用这种写法。
使用if not x这种写法的前提是:必须清楚x等于None, False, 空字符串”“, 0, 空列表[], 空字典{}, 空元组()时对你的判断没有影响才行。
================================================================
不过这并不适用于变量是函数的情况,以下转载自:https://github.com/wklken/stackoverflow-py-top-qa/blob/master/contents/qa-control-flow.md
foo is None 和 foo == None的区别
问题 链接
if foo is None: passif foo == None: pass
如果比较相同的对象实例,is总是返回True 而 == 最终取决于 “eq()”
>>> class foo(object): def __eq__(self, other): return True>>> f = foo()>>> f == NoneTrue>>> f is NoneFalse>>> list1 = [1, 2, 3]>>> list2 = [1, 2, 3]>>> list1==list2True>>> list1 is list2False
另外
(ob1 is ob2) 等价于 (id(ob1) == id(ob2))
################################################################################
补充,2013.10.09
转自http://zhidao.baidu.com/question/514056244.html
python中的not具体表示是什么,举个例子说一下,衷心的感谢
在python中not是逻辑判断词,用于布尔型True和False,not True为False,not False为True,以下是几个常用的not的用法:(1) not与逻辑判断句if连用,代表not后面的表达式为False的时候,执行冒号后面的语句。比如:a = Falseif not a: (这里因为a是False,所以not a就是True) print "hello"这里就能够输出结果hello(2) 判断元素是否在列表或者字典中,if a not in b,a是元素,b是列表或字典,这句话的意思是如果a不在列表b中,那么就执行冒号后面的语句,比如:a = 5b = [1, 2, 3]if a not in b: print "hello"这里也能够输出结果hello
not x 意思相当于 if x is false, then True, else False
转自博客链接:
http://blog.csdn.net/sasoritattoo/article/details/12451359
- python代码`if not x:` 和`if x is not None:`和`if not x is None:`使用
- python代码`if not x:` 和`if x is not None:`和`if not x is None:`使用
- python代码`if not x:` 和`if x is not None:`和`if not x is None:`使用
- python代码`if not x:` 和`if x is not None:`和`if not x is None:`使用
- python代码`if not x:` 和`if x is not None:`和`if not x is None:`使用
- python代码`if not x:` 和`if x is not None:`和`if not x is None:`使用
- python代码`if not x:` 和`if x is not None:`和`if not x is None:`使用
- python is not None
- If-Modified-Since & If-None-Match x-wap-profile
- mysql if 、ifnull、is not null 和isnull函数
- IF OBJECT_ID('UP_GET_XXX_SEQNO') IS NOT NULL
- 【LuKS】Vba if not c is nothing
- cocos2d-x 3.2使用VS2015编译报错:msb3073 代码9009error MSB3073: 命令“if not exist"【by徐玉丽】
- onblur和onclick的冲突解决原名:onBlur killing onClick but not if setTimeout is used
- if exists和if not exists关键字用法
- if exists和if not exists关键字用法
- if exists和if not exists关键字用法
- Python中 if not 用法
- 常用正则表达式
- docker学习笔记02:docker镜像
- mysql的timestamp类型
- JFreeChart生成的图片在IE和360浏览器上不显示
- JdbcTemplate
- python代码`if not x:` 和`if x is not None:`和`if not x is None:`使用
- Java对【JSON数据的解析】--Gson解析法
- 自定义TextView 灵活使用Shape实现边框
- 工业控制信息安全资源汇总(国内篇)
- 关于css选择器的小结
- C++第4次实验(基础班)—循环结构程序设计(参考答案)-项目3:乘法口诀表
- eclipse中git分支创建与合并
- 案例十四、css3和fullpage实现全屏滚动
- Binding基础