美文网首页
Life is short, you need Python

Life is short, you need Python

作者: 冬风十里Y | 来源:发表于2018-04-22 16:07 被阅读46次

[TOC]

关于if not x:&if x is not None&if not x is None

代码中经常会有变量是否为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  

在python中 None, False, 空字符串"", 0, 空列表[], 空字典{}, 空元组()都相当于False ,即:

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是否为None,但是却把x==[]的情况也判断进来了,此种情况下将无法区分。
对于习惯于使用if not x这种写法的pythoner,必须清楚x等于None, False, 空字符串"", 0, 空列表[], 空字典{}, 空元组()时对你的判断没有影响才行。

而对于if x is not Noneif not x is None写法,很明显前者更清晰,而后者有可能使读者误解为if (not x) is None,因此推荐前者,同时这也是谷歌推荐的风格

结论:

if x is not None是最好的写法,清晰,不会出现错误,以后坚持使用这种写法。

使用if not x这种写法的前提是:必须清楚x等于None, False, 空字符串"", 0, 空列表[], 空字典{}, 空元组()时对你的判断没有影响才行。

Python if 和 for 的多种写法

1.not in

>>> a=2

>>> a not in [2,3,4]
False
>>> a in [2,3,4] 
  1. c if a else b #这里注意,一定要有b,而且b不能为pass
>>> a=3 if 2>3 else 4
>>> a
4

>>> a=3 if 2<3 else 4 
>>> a

3.[fun(a) for a in [...]]

>>> [a+1 for a in [2,3,4,5,6]]
[3, 4, 5, 6, 7]

4.a,b=b,a

>>> a=1
>>> b=2
>>> a,b=b,a
>>> a
2
>>> b
1

5.'内容'.join([string array])

>>> '.'.join[2,3,4,5,6]  
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'
>>> '.'.join([2,3,4,5,6]) 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sequence item 0: expected string, int found
>>> '.'.join(['2','3','4','5','6']) 
'2.3.4.5.6'

参考资料:

python代码if not x:if x is not None:if not x is None:使用
python里面的几个用法,not in,c if a else b,[fun(a) for a in [...]] , a,b=b,a,'内容'.join([string array])
Python if 和 for 的多种写法

相关文章

  • 01.Python简介

    01.Python简介 Python 简介 “Life is short, you need Python!” —...

  • 常瑞贤2018-06-14

    人生苦短,我用 Python —— Life is short, you need Python Python 的...

  • python

    人生苦短,我用 Python —— Life is short, you need Python Python可以...

  • 01. 认识Python

    人生苦短,我用 Python —— Life is short, you need Python Python 的...

  • Python的学习过程

    人生苦短,我用Python-------Life is short, you need Python Python...

  • 无标题文章

    python : 人生苦短,我用python ------Life is short ,you need pyth...

  • 笨办法学Python

    背景 " Life is short, you need Python——Bruce Eckel " 【人生苦短,...

  • 认识Python

    人生苦短,我用Python —— Life is short, you need Python 1.Python的...

  • Python http 基础 不知道在哪找的

    认识 Python 人生苦短,我用 Python —— Life is short, you need Pytho...

  • 01 认识 Python

    认识 Python 人生苦短,我用 Python —— Life is short, you need Pytho...

网友评论

      本文标题:Life is short, you need Python

      本文链接:https://www.haomeiwen.com/subject/lrrclftx.html