python learning
2016-06-24 09:25
393 查看
up vote7down vote | NoneTypeis simply the type of the Nonesingleton: >>> type(None) <type 'NoneType'> From the latter link above: None The sole value of the type NoneType. Noneis frequently used to represent the absence of a value, as when default arguments are not passed to a function. Assignments to Noneare illegal and raise a SyntaxError. |
what about
Python
`if x is not None` or `if not x is None`?
There's no performance difference, as they compile to the same bytecode:Python 2.6.2 (r262:71600, Apr 15 2009, 07:20:39) >>> import dis >>> def f(x): ... return x is not None ... >>> dis.dis(f) 2 0 LOAD_FAST 0 (x) 3 LOAD_CONST 0 (None) 6 COMPARE_OP 9 (is not) 9 RETURN_VALUE >>> def g(x): ... return not x is None ... >>> dis.dis(g) 2 0 LOAD_FAST 0 (x) 3 LOAD_CONST 0 (None) 6 COMPARE_OP 9 (is not) 9 RETURN_VALUE
Stylistically, I try to avoid
not x is y. Although the compiler will always treat it as
not (x is y), a human reader might misunderstand the construct as
(not x) is y. If I write
x is not ythen there is no ambiguity.
相关文章推荐
- (python)创建一个可以比较的自定义类
- Python函数
- 机器学习经典算法详解及Python实现–决策树(Decision Tree)
- python学习笔记-POST方式
- python之metaclass+singleton(三)
- Kaggle digit-recognizer PCA+SVM
- python-文件压缩和解压
- python-configparser模块
- RabbitMQ+Python入门经典] 兔子和兔子窝
- [Python Scrapy] Windows下BeautifulSoup安装
- __name__的认识
- Python实现信用卡系统(支持购物、转账、存取钱)
- 深入了解Python数据类型之列表
- 深入解析Python编程中super关键字的用法
- Python的装饰器用法学习笔记
- Python中的defer库
- detect the encoding of files in Python
- python 面向对象进阶篇
- python学习-day1
- Exec 与 eval 语句