python中if not是什么意思,python if not不同应用有什么区别,pythonifnot区别,python 中 "if...
python if not不同应用有什么区别,pythonifnot区别,python 中 "if
python 中 "if x is not None" 和 "if not x is None" 有什么区别?
google 的 python 风格指南中这么说:Beware of writing if x: when you really mean if x is not None:—e.g., when testing whether a variable or argument that defaults to None was set to some other value. The other value might be a value that's false in a boolean context!
也就是说,推荐使用 if x is not None 进行判断,but why?
if not x is None 和 if x is not None 對計算機而言是一樣的。對人類而言是不一樣的。
前者的隱含意義是x本該是None結果不是,後者是x不該是None結果也不是。個人感覺,無客觀依據(好像沒有人做這樣的心理實驗?)。
前者更pythonic
内容和标题不符,if x 和 if not x is None 是不一样的。
if x 会对x做 __nonzero__ 判断,当 x 为 ''(空字符串),{}(空字典), 0 的时候都是 False。当你确实要判断一个变量不是 None 的时候,应该用 if x is not None。
至于 if not x is None 和 if x is not None 是一样的,选一个你读的顺的就好。
前者更接近白话文,而后者有可能使读者误解为if (not x) is None.
首先,直接查看操作码(XP+Python3.4)。
x is not None的操作码:dis.dis('if x is not None: pass') 0 LOAD_NAME 0 (x) 3 LOAD_CONST 0 (None) 6 COMPARE_OP 9 (is not) 9 POP_JUMP_IF_FALSE 15 12 JUMP_FORWARD 0 (to 15)>> 15 LOAD_CONST 0 (None) 18 RETURN_VALUE
if not x is None的操作码:dis.dis('if not x is None: pass') 0 LOAD_NAME 0 (x) 3 LOAD_CONST 0 (None) 6 COMPARE_OP 9 (is not) 9 POP_JUMP_IF_FALSE 15 12 JUMP_FORWARD 0 (to 15)>> 15 LOAD_CONST 0 (None) 18 RETURN_VALUE
可以看到,操作码是一样的!
题主还可以测试后面加or或者and的情况。
个人意见,if x is not None比if not x is None更加易读,毕竟英语当中有一个 isn't 呢。
假如原本你的x为None
你要执行如下代码判断x是否已经发生改变,仍为None?x = Falseif x: print 'yes'else: print 'no'
你得到会是 no ,其实 x 已经被改变了,但仍然是False
if not x is None 和 if x is not None 结果是一样的。
if not x is None => if not (x is None)
if x is not None => if x (is not) None
编橙之家文章,
python中if not是什么意思,python if not不同应用有什么区别,pythonifnot区别,python 中 "if...相关推荐
- python使用matplotlib可视化线图(line plot)、使用arrow函数在matplotlib可视化图像中添加箭头(drawing arrows in matplotlib)
python使用matplotlib可视化线图(line plot).使用arrow函数在matplotlib可视化图像中添加箭头(drawing arrows in matplotlib) 目录
- python中append函数合并列表且列表内数字从高到低_35个高级Python知识点总结
No.1 一切皆对象 众所周知,Java中强调"一切皆对象",但是Python中的面向对象比Java更加彻底,因为Python中的类(class)也是对象,函数(function) ...
- python编程项目中遇到的困难_python开发中遇到的常见问题,你们又是如何解决的呢?如何快速的学好python,有没有什么小技巧?...
一.expected an indented block (小白问题)缩进问题, 报错地方前面要有4个空格 二. module 'urllib' has no attribute 'request' ...
- python在不同系统代码区别_python不同系统中打开方法
在Linux.Windows.Mac OS的命令行窗口或Shell窗口,执行python命令,启动Python交互式解释器.交互式解释器会等待用户输入Python语句.输入Python语句并回车,解释 ...
- Python爬虫:爬取知乎上的视频,并把下载链接保存到md文件中
Python爬虫:爬取知乎上的视频,并把下载链接保存到md文件中 1.需要的Python模块 主要是requests模块,用于得到的网页的数据 安装命令为:pip install requests 2 ...
- 【爬虫】利用Python爬虫爬取小麦苗itpub博客的所有文章的连接地址并写入Excel中(2)...
[爬虫]利用Python爬虫爬取小麦苗itpub博客的所有文章的连接地址并写入Excel中(2) 第一篇( http://blog.itpub.net/26736162/viewspace-22865 ...
- ROS中的TF坐标变换工具及实现、Rviz查看(十四)C++、python
目录 TF坐标变换引言 概述 概念 坐标msg消息 简介 geometry_msgs/TransformStamped geometry_msgs/PointStamped 静态坐标变换 简介 C++ ...
- python中的数据类型有哪些是可阅读_Python list data type(list)[学习Python的必要基础知识][阅读本文],列表,数据类型,必备,看此,一篇,就够,了...
您的"关注"和"点赞",是信任,是认可,是支持,是动力- 如意见相佐,可留言. 本人必将竭尽全力试图做到准确和全面,终其一生进行修改补充更新. 1 Python ...
- python从键盘输入一个列表计算输出元素的平均值_python列表查找值_在Python中查找列表平均值的5种方法...
python列表查找值 Hi Folks! In this article, we will have a look at the various ways to find the average o ...
最新文章
- jsp error-page没有生效
- 几种可直接使用的架构模式及其使用场景
- mysql connections参数_MySQL性能优化之max_connections配置参数浅析
- JavaScript判断浏览器类型及版本
- MyBatis批量插入
- 011_SpringBoot视图层技术thymeleaf-日期格式化
- python 笔记:time calendar
- order by case when
- 颜宁课题组再发Cell!1个月时间内4篇顶刊!
- [渝粤题库]陕西师范大学《幼儿园社会教育》作业
- 如何在多web服务器共享SESSION数据
- Python联网下载文件
- JVM——Java内存模型(JMM)
- 在Apache服务器上安装SSL证书
- MyBatis-Spring-Boot 使用总结
- zipkin实战(python)
- 化学堵水剂处理油井出水问题
- python屏幕找图_Python实现按键精灵(二)-找图找色
- MYSQL入门基础知识
- 大型电子病历系统【整套源代码分享】