您的位置:首页 > 编程语言 > Python开发

python learning

2016-06-24 09:25 393 查看
up vote7down
vote
NoneType
 is
simply the type of the 
None
 singleton:
>>> type(None)
<type 'NoneType'>


From the latter link above:

None


The sole value of the type 
NoneType
None
 is
frequently used to represent the absence of a value, as when default arguments are not passed to a function. Assignments to 
None
 are
illegal and raise a 
SyntaxError
.

任何类型的变量都可以使用None判断是否初始化

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 y
then there is no ambiguity.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: