美文网首页
2020-09-23

2020-09-23

作者: FravelWang | 来源:发表于2020-09-23 16:44 被阅读0次

https://www.cnblogs.com/devops-qxw/p/9641316.html

python中的not具体表示是什么:

在python中not是逻辑判断词,用于布尔型True和False,not True为False,not False为True,以下是几个常用的not的用法:

(1) not与逻辑判断句if连用,代表not后面的表达式为False的时候,执行冒号后面的语句。比如:

a = False

if not a: (这里因为a是False,所以not a就是True)

print "hello"

这里就能够输出结果hello

(2) 判断元素是否在列表或者字典中,if a not in b,a是元素,b是列表或字典,这句话的意思是如果a不在列表b中,那么就执行冒号后面的语句,比如:

a = 5

b = [1, 2, 3]

if a not in b:

print "hello"

这里也能够输出结果hello

not x 意思相当于 if x is false, then True, else False

<pre style="margin: 0px; padding: 0px; color: rgb(0, 0, 0); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">代码中经常会有变量是否为None的判断,有三种主要的写法:

第一种是if x is None

第二种是 if not x:

第三种是if not x is None(这句这样理解更清晰if not (x is None)) 。

如果你觉得这样写没啥区别,那么你可就要小心了,这里面有一个坑。先来看一下代码:

1.  >>> x = 1  
2.  >>> not x  
3.  False  
4.  >>> x = [1]  
5.  >>> not x  
6.  False  
7.  >>> x = 0  
8.  >>> not x  
9.  True  
10.  >>> x = [0]         # You don't want to fall in this one.  
11.  >>> not x  
12.  False  

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

[python] view plaincopy

  1. not None == not False == not '' == not 0 == not [] == not {} == not ()

因此在使用列表的时候,如果你想区分x==[]和x==None两种情况的话, 此时if not x:将会出现问题:

1.  >>> x = []  
2.  >>> y = None  
3.  >>>   
4.  >>> x is None  
5.  False  
6.  >>> y is None  
7.  True  
8.  >>>   
9.  >>>   
10.  >>> not x  
11.  True  
12.  >>> not y  
13.  True  
14.  >>>   
15.  >>>   
16.  >>> not x is None  
17.  >>> True  
18.  >>> not y is None  
19.  False  
20.  >>>   

也许你是想判断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, 空列表[], 空字典{}, 空元组()时对你的判断没有影响才行。

================================================================
不过这并不适用于变量是函数的情况,以下转载自:https://github.com/wklken/stackoverflow-py-top-qa/blob/master/contents/qa-control-flow.md

foo is None 和 foo == None的区别

问题 链接

  1. if foo is None: pass

  2. if foo == None: pass

如果比较相同的对象实例,is总是返回True 而 == 最终取决于 "eq()"

1.  >>> class foo(object):

2.  def __eq__(self, other):

3.  return True

5.  >>> f = foo()

6.  >>> f == None

7.  True

8.  >>> f is None

9.  False

11.  >>> list1 = [1, 2, 3]

12.  >>> list2 = [1, 2, 3]

13.  >>> list1==list2

14.  True

15.  >>> list1 is list2

16.  False

另外

(ob1 is ob2) 等价于 (id(ob1) == id(ob2))

相关文章

  • 划词翻译:沙拉查词+quicker

    title: 划词翻译:沙拉查词+quickerdate: 2020-09-23 16:11:24categori...

  • java springboot 生成pdf 亲测可用 附源码

    实验时间: 2020-09-23代码: https://github.com/mefly521/springbo...

  • js日期格式化(yy-mm-dd和yy-mm-dd hh:mm:

    /** 时间格式化 将 2020-09-23T11:54:16.000+0000 格式化成 2020-09-23 ...

  • 西安自驾游攻略

    2020-09-23更新: 出发前需要准备的东西: 1.证件类:身份证/学生证/驾照,适量现金; 2.生活类:保温...

  • 2020-09-23

    2020-09-23 姓名 :曹静杰 企业名称 : 辽宁辽阳丛迪服装有限公司 组别 388期 反省1组 志工529...

  • 2020-09-23

    2020-09-23 姓名 :曹静杰 企业名称 : 辽宁辽阳丛迪服装有限公司 组别 388期 反省1组 志工529...

  • 1#树洞

    2020-09-23今天心情很复杂,想了很多事情以至于没有心思工作学习。晚上去游了一半个小时的泳感觉身体很放松,这...

  • 暹罗第一次打疫苗

    2020-09-23带着暹罗打疫苗去了2个月的时候1.35kg3个月的体重2.35kg医生注射完疫苗(妙三多)以后...

  • 日子越来越难过

    2020-09-23 所在单位在前期辞退了10位聘用人员——大多是我的同事。她们中的大多数,我分析了一下,就是经常...

  • 用心去画

    2020-09-23随笔 晚上,收拾好家务活,伺候孩子睡觉后。一心想画画,又怕一旦开始,就停不下来,因为现在已经是...

网友评论

      本文标题:2020-09-23

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