对 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 ...
随机推荐
- 微信公众平台自动回复wechatlib.jar的生成及wechatlib解析
微信公众平台出来有一段时日了,官方提供的自动回复的接口调用大致是这么些类型(text/image/location/link),每个项目都如此拷贝代码,在笔者看来比较麻烦,今天乘着点闲暇的时间特意将这 ...
- SQL SERVER 2008 R2数据库出现“远程过程调用失败”(0x800706be)错误,怎么办!!
以前SQL Server 2008 不能登陆的时候,总是通过“计算机管理”→“SQL Server服务”更改一下,"SQL Server(MSSQLSERVER)". 可是现在出现 ...
- RequireJS+JQueryMobile
RequireJS提供了JS下模块化开发的充分条件.之前我自己也在多个项目中尝试模块化开发,但是由于没有类似RequireJS这样的框架,最后的效果都不是很理想. 在RequireJS中,所有的JS都 ...
- 全文检索学习历程目录结构(Lucene、ElasticSearch)
1.目录 (1) Apache Lucene(全文检索引擎)—创建索引:http://www.cnblogs.com/hanyinglong/p/5387816.html (2) Apache Luc ...
- javascript中的一些核心知识点以及需要注意的地方
前言 近期杂事甚多,这些事情的积累对知识体系的提升有好处,但是却不能整理出来,也整理不出来 比如说我最近研究的Hybrid在线联调方案便过于依赖于业务,就算分享也不会有人读懂,若是抽一点来分享又意义不 ...
- 火狐浏览器如何js关闭窗口的几种解决方法
今天在项目上有一个页面要求在几秒后自动关闭,想着还比较简单,用window.close()就可以了,但是用IE/谷歌/火狐浏览器试了一下,发现IE可以,谷歌用网上的兼容方法也可以实现,但是火狐这里卡住 ...
- IIS10中使用OpenSSL来创建CA并且签发SSL证书
参考: http://www.cnblogs.com/lierle/p/5140187.html http://alvinhu.com/blog/2013/06/12/creating-a-certi ...
- Sanboxie 5.14安装图解
Sanboxie, 即沙盘,引用官方解释:电脑就像一张纸,程序的运行与改动,就像将字写在纸上.而Sandboxie就相当于在纸上放了块玻璃,程序的运行与改动就像写在了那块玻璃上,除去玻璃,纸上还是一点 ...
- iOS开发-UI 从入门到精通(五)
近日在做项目的时候,为了快捷适配屏幕采用了Storyboard,添加约束以后运行后发现一个问题(下面将以普通案例展示该问题);在4.7 甚至更大的屏幕下是没有问题的,如下图(4.7屏幕): 但是放到更 ...
- Android measure过程分析
作为一名Android开发人员,我们都知道一个View从无到有,会经历3个阶段: 1. measure/测量阶段,也就是确定某个view大小的过程: 2. layout/布局阶段,也就是确定其左上右下 ...