对 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-in-operator-use-cases  

对 Python 语法不够了解导致的 bug的更多相关文章

  1. (数据分析)第02章 Python语法基础,IPython和Jupyter Notebooks.md

    第2章 Python语法基础,IPython和Jupyter Notebooks 当我在2011年和2012年写作本书的第一版时,可用的学习Python数据分析的资源很少.这部分上是一个鸡和蛋的问题: ...

  2. 三、Python语法介绍

    三.Python语言介绍 3.1.了解Python语言 Python 是1989 年荷兰人 Guido van Rossum (简称 Guido)在圣诞节期间为了打发时间,发明的一门面向对象的解释性编 ...

  3. python 笔记2:python语法基础

    python语法学习笔记: 1 输入输出 input(),print(). name = input('input your name : ')print('hello ,'+name)print(& ...

  4. python语法快速入门(1)

    http://www.runoob.com/python/python-tutorial.html Python 是一种解释型语言: 这意味着开发过程中没有了编译这个环节.类似于PHP和Perl语言 ...

  5. python语法笔记(四)

    1.对象的属性     python一切皆对象,每个对象都可能有多个属性.python的属性有一套统一的管理方案. 属性的__dict__系统     对象的属性可能来自于其类定义,叫做类属性:还可能 ...

  6. MySQL中char(36)被认为是GUID导致的BUG及解决方案

    MySQL中char(36)被认为是GUID导致的BUG及解决方案 有时候在使用Toad或在程序中,偶尔会遇到如下的错误: System.FormatException GUID 应包含带 4 个短划 ...

  7. 请慢慢移动……由于操作快慢导致的bug

    最近的工作中,遇到一个由于操作快慢不同导致的bug,原因是,操作慢的时候程序接收到了停止操作,继续处理正确,而快速操作的时候程序来不及处理操作停止的动作,导致需要传入的数据值已经改变,程序报错.这种缺 ...

  8. python语法-[with来自动释放对象]

    python语法-[with来自动释放对象] http://www.cnblogs.com/itech/archive/2011/01/13/1934779.html 一 with python中的w ...

  9. wxpython 支持python语法高亮的自定义文本框控件的代码

    在研发闲暇时间,把开发过程中比较重要的一些代码做个珍藏,下面的代码内容是关于wxpython 支持python语法高亮的自定义文本框控件的代码,应该是对大家也有用. import keywordimp ...

随机推荐

  1. 20、ASP.NET MVC入门到精通——WebAPI

    本系列目录:ASP.NET MVC4入门到精通系列目录汇总 微软有了Webservice和WCF,为什么还要有WebAPI? 用过WCF的人应该都清楚,面对那一大堆复杂的配置文件,有时候一出问题,真的 ...

  2. Oracle常用SQL查询

    一.ORACLE的启动和关闭 1.在单机环境下要想启动或关闭oracle系统必须首先切换到oracle用户,如下: su - oracle a.启动Oracle系统 oracle>svrmgrl ...

  3. 学习zepto.js(对象方法)[5]

    继续说. clone: 该方法不接收任何参数,会返回对象中的所有元素集合,但不会对象绑定的事件. var $temp = $("div").clone(); //并不接收任何参数. ...

  4. C++序列化、反序列化

    几个常见的库 http://stackoverflow.com/questions/3637581/fastest-c-serialization Boost: Fast, assorted C++ ...

  5. 【Swift 2.1】为 UIView 添加点击事件和点击效果

    前言 UIView 不像 UIButton 加了点击事件就会有点击效果,体验要差不少,这里分别通过自定义和扩展来实现类似 UIButton 的效果. 声明 欢迎转载,但请保留文章原始出处:) 博客园: ...

  6. Feathers UI 性能优化

    Feathers UI作者写的 http://joshblog.net/2013/feathers-performance-tips/

  7. 设置ASP.NET MVC站点默认页为.html页 .

    同事部署了一个Asp.Net MVC的站点,希望它的默认页是index.html页,在vs2010中给站点根目录增加了index.html,然后调用没有什么问题,但部署到IIS7上,在功能试图=> ...

  8. .NET程序员项目开发必知必会—Dev环境中的集成测试用例执行时上下文环境检查(实战)

    Microsoft.NET 解决方案,项目开发必知必会. 从这篇文章开始我将分享一系列我认为在实际工作中很有必要的一些.NET项目开发的核心技术点,所以我称为必知必会.尽管这一系列是使用.NET/C# ...

  9. Windows 安装Kafka

    Windows 7 安装Apache kafka_2.11-0.9.0.1     下载所需文件 Zookeeper: http://www.apache.org/dyn/closer.cgi/zoo ...

  10. Navicat安装详解

    本文章介绍MySql图形化操作软件Navicat的安装 属于PHP环境搭建的一部分. PHP完整配置信息请参考 http://www.cnblogs.com/azhe-style/p/php_new_ ...