对 Python 语法不够了解导致的 bug
对 Python 语法不够了解导致的 bug。
`in`
'20' in '11264,6144,4096,3072,2048,1024,300,30'
Out[7]:
True
a_list = '11264,6144,4096,3072,2048,1024,300,30'.split(',')
a_list
Out[10]:
['11264', '6144', '4096', '3072', '2048', '1024', '300', '30']
'20' in a_list
Out[11]:
False
然后我去搜索 python `in`,有很多噪音,因为 `in` 这个词出现太多了。然后想到 Python 自带的 help,于是我在Python shell 中写
help('in')
Membership test operations
**************************
The operators "in" and "not in" test for membership. "x in s"
evaluates to true if *x* is a member of *s*, and false otherwise. "x
not in s" returns the negation of "x in s". All built-in sequences
and set types support this as well as dictionary, for which "in" tests
whether the dictionary has a given key. For container types such as
list, tuple, set, frozenset, dict, or collections.deque, the
expression "x in y" is equivalent to "any(x is e or x == e for e in
y)".
For the string and bytes types, "x in y" is true if and only if *x* is
a substring of *y*. An equivalent test is "y.find(x) != -1". Empty
strings are always considered to be a substring of any other string,
so """ in "abc"" will return "True".
For user-defined classes which define the "__contains__()" method, "x
in y" is true if and only if "y.__contains__(x)" is true.
For user-defined classes which do not define "__contains__()" but do
define "__iter__()", "x in y" is true if some value "z" with "x == z"
is produced while iterating over "y". If an exception is raised
during the iteration, it is as if "in" raised that exception.
Lastly, the old-style iteration protocol is tried: if a class defines
"__getitem__()", "x in y" is true if and only if there is a non-
negative integer index *i* such that "x == y[i]", and all lower
integer indices do not raise "IndexError" exception. (If any other
exception is raised, it is as if "in" raised that exception).
The operator "not in" is defined to have the inverse true value of
"in".
Related help topics: SEQUENCEMETHODS
出现的解释很有帮助。所以对于 built-in 的关键字或者函数,我们应首先使用自带的 help 进行查询,而不是去谷歌搜索。
引申出 is 与 == 的区别
5 is 5
Out[16]:
True
5 == 5
Out[17]:
True
nan = float('NaN')
nan is nan
Out[19]:
True
nan == nan
Out[20]:
False
参考
对 Python 语法不够了解导致的 bug的更多相关文章
- (数据分析)第02章 Python语法基础,IPython和Jupyter Notebooks.md
第2章 Python语法基础,IPython和Jupyter Notebooks 当我在2011年和2012年写作本书的第一版时,可用的学习Python数据分析的资源很少.这部分上是一个鸡和蛋的问题: ...
- 三、Python语法介绍
三.Python语言介绍 3.1.了解Python语言 Python 是1989 年荷兰人 Guido van Rossum (简称 Guido)在圣诞节期间为了打发时间,发明的一门面向对象的解释性编 ...
- python 笔记2:python语法基础
python语法学习笔记: 1 输入输出 input(),print(). name = input('input your name : ')print('hello ,'+name)print(& ...
- python语法快速入门(1)
http://www.runoob.com/python/python-tutorial.html Python 是一种解释型语言: 这意味着开发过程中没有了编译这个环节.类似于PHP和Perl语言 ...
- python语法笔记(四)
1.对象的属性 python一切皆对象,每个对象都可能有多个属性.python的属性有一套统一的管理方案. 属性的__dict__系统 对象的属性可能来自于其类定义,叫做类属性:还可能 ...
- MySQL中char(36)被认为是GUID导致的BUG及解决方案
MySQL中char(36)被认为是GUID导致的BUG及解决方案 有时候在使用Toad或在程序中,偶尔会遇到如下的错误: System.FormatException GUID 应包含带 4 个短划 ...
- 请慢慢移动……由于操作快慢导致的bug
最近的工作中,遇到一个由于操作快慢不同导致的bug,原因是,操作慢的时候程序接收到了停止操作,继续处理正确,而快速操作的时候程序来不及处理操作停止的动作,导致需要传入的数据值已经改变,程序报错.这种缺 ...
- python语法-[with来自动释放对象]
python语法-[with来自动释放对象] http://www.cnblogs.com/itech/archive/2011/01/13/1934779.html 一 with python中的w ...
- wxpython 支持python语法高亮的自定义文本框控件的代码
在研发闲暇时间,把开发过程中比较重要的一些代码做个珍藏,下面的代码内容是关于wxpython 支持python语法高亮的自定义文本框控件的代码,应该是对大家也有用. import keywordimp ...
随机推荐
- 事件流之事件冒泡与事件捕获<JavaScript高级程序设计>学习笔记
1.事件流 浏览器开发团队遇到一个很有意思问题:页面的那一部分会拥有特定的事件? 对于理解这个问题您可以想象画在一张纸上的一组同心圆,如果你把手指放在圆心上,那么你的手指指向的其实不是一个圆,而是纸上 ...
- 【单页应用】view与model相关梳理
前情回顾 根据之前的学习,我们形成了一个view与一个messageCenterview这块来说又内建了一套mvc的东西,我们这里来理一下首先View一层由三部分组成:① view② dataAdpt ...
- iOS开发中<null>的处理
在iOS开发过程中经常需要与服务器进行数据通讯,JSON就是一种常用的高效简洁的数据格式. 问题: 在项目中,一直遇到一个坑的问题,程序在获取某些数据之后莫名崩溃.原因是:由于服务器的数据库中有些字段 ...
- iOS中的交换空间(swap space)
看来是没有交换空间,原因是闪存和SSD硬盘相比,速度很慢,也有电源管理的原因. the NAND flash is not designed to be used as swap. It is dam ...
- 二进制配置文件为什么比json等配置文件效率高
二进制配置文件为什么比json等配置文件高效 项目中用spine做动画,spine可以导出json和二进制的动画配置文件,蛋疼的是spine官方竟然没有提供c的二进制配置解析代码,更没有提供它二进制文 ...
- Android自定义View4——统计图View
1.介绍 周末在逛慕课网的时候,看到了一张学习计划报告图,详细记录了自己一周的学习情况,天天都是0节课啊!正好在学习Android自定义View,于是就想着自己去写了一个,这里先给出一张慕课网的图,和 ...
- FMDB第三方框架
FMDB是同AFN,SDWebImage同样好用的第三方框架,它以OC的方式封装了SQLite的C语言API,使得开发变得简单方便. 附上github链接https://github.com/ccgu ...
- iOS 获取相册中图片的名字 url
__block NSString *imageFileName; NSURL *imageURL = [info valueForKey:UIImagePickerControllerReferenc ...
- ASP.NET获取真正的客户端IP地址的6种方法
Request.ServerVariables("REMOTE_ADDR") 来取得客户端的IP地址,但如果客户端是使用代理服务器来访问,那取到的就是代理服务器的IP地址,而不是真 ...
- #研发解决方案介绍#基于StatsD+Graphite的智能监控解决方案
郑昀 基于李丹和刘奎的文档 创建于2014/12/5 关键词:监控.dashboard.PHP.graphite.statsd.whisper.carbon.grafana.influxdb.Pyth ...