假设有如下代码:

for i in range(10):
if i == 5:
print 'found it! i = %s' % i
else:
print 'not found it ...'

你期望的结果是,当找到5时打印出:

found it! i = 5

实际上打印出来的结果为:

found it! i = 5
not found it ...

显然这不是我们期望的结果。

根据官方文档说法:

>When the items are exhausted (which is immediately when the sequence is empty), the suite in the else clause, if present, is executed, and the loop terminates.

>A break statement executed in the first suite terminates the loop without executing the else clause’s suite. A continue statement executed in the first suite skips the rest of the suite and continues with the next item, or with the else clause if there was no next item.

https://docs.python.org/2/reference/compound_stmts.html#the-for-statement

大意是说当迭代的对象迭代完并为空时,位于else的子句将执行,而如果在for循环中含有break时则直接终止循环,并不会执行else子句。

所以正确的写法应该为:

for i in range(10):
if i == 5:
print 'found it! i = %s' % i
break
else:
print 'not found it ...'

当使用pylint检测代码时会提示 Else clause on loop without a break statement (useless-else-on-loop)

所以养成使用pylint检测代码的习惯还是很有必要的,像这种逻辑错误不注意点还是很难发现的。

Python中for else注意事项的更多相关文章

  1. python中除法的注意事项

    使用python数据处理,代码如下: import numpy as np fs = 5 ts = np.arange(-1,1+1/fs,1/fs) 发现了这样一个错误: Traceback (mo ...

  2. Navicat的使用与python中使用MySQL的基本方法

    Navicat的使用与python中使用MySQL的基本方法 Navicat的下载及安装 下载地址 http://www.navicat.com.cn/download/navicat-premium ...

  3. python中的join.set ,copy以及删除注意事项:

    1 ,join  :  将对象以字符串的方式拼接成一个整体 for E :  li = ["李白", "是", "诗仙"]    s = & ...

  4. python中的注意事项

    .python 中的 and 从左到右计算表达式,若所有值均为真,则返回最后一个值,若存在假,返回第一个假值: or 也是从左到有计算表达式,返回第一个为真的值: 其中数字 0 是假,其他都是真: 字 ...

  5. 【python】继承时注意事项

    1. __init__ 注意事项 如果父类有__init__函数,子类没有,则子类自动调用父类__init__函数 如果父类有__init__函数,子类也有,则子类必须主动调用父类__init__函数 ...

  6. [转载]python中multiprocessing.pool函数介绍

    原文地址:http://blog.sina.com.cn/s/blog_5fa432b40101kwpi.html 作者:龙峰 摘自:http://hi.baidu.com/xjtukanif/blo ...

  7. 【转载】Python中如何高效实现两个字典合并,三种方法比较。

    本文转载自:http://www.pythoner.com/13.html Python中将两个字典进行合并操作,是一个比较常见的问题.本文将介绍几种实现两个字典合并的方案,并对其进行比较. 对于这个 ...

  8. python中multiprocessing.pool函数介绍_正在拉磨_新浪博客

    python中multiprocessing.pool函数介绍_正在拉磨_新浪博客     python中multiprocessing.pool函数介绍    (2010-06-10 03:46:5 ...

  9. 如何导入python中的模块

    作为一名新手Python程序员,你首先需要学习的内容之一就是如何导入模块或包.但是我注意到,那些许多年来不时使用Python的人并不是都知道Python的导入机制其实非常灵活.在本文中,我们将探讨以下 ...

随机推荐

  1. javascript中some,every,map,filter是只用和ansyc中的each,eachLimit,map,mapLImit,filter的使用

    var t = [1,2,3,4,5]; //some找到数组中第一个符合要求的值后就不在继续执行//用来判断数组中是否存符合要求的值,返回结果true|false//function返回类型为boo ...

  2. vs 2005/2008/2010 ATL ActiveX控件显示XP风格

    vs 2005/2008/2010 ATL ActiveX控件在IE浏览器中控件显示的是原始的风格,要显示XP风格只要进行以下操作 一.在Stdafx.h中增加下以内容 #if defined _M_ ...

  3. 28 python 并发编程之多进程

    一 multiprocessing模块介绍 python中的多线程无法利用多核优势,如果想要充分地使用多核CPU的资源(os.cpu_count()查看),在python中大部分情况需要使用多进程.P ...

  4. hdu-5183-Negative and Positive (NP)(hash模板)

    题目链接 #include <iostream> #include <cstdio> #include <cstring> #include <algorit ...

  5. 搞定thrift双向消息

    thrift作为脱胎于facebook的rpc框架,各方面都非常优秀.清晰的分层设计,多语言的支持,以及不输protocolbuffer的效率(compact下优于protocolbuffer),都让 ...

  6. fastCGI模块

    这个模块允许nginx同FastCGI协同工作,并且控制哪些参数将被安全传递.例: location / { fastcgi_pass localhost:9000; fastcgi_index in ...

  7. 关于 freetds pymssql 的安装部署

    关于 freetds pymssql 的安装部署一.安装: (freetds-0.91 pymssql 2.0.1) 如果要在linux机器 连mysqlsever 1.需要安装freetds./co ...

  8. 我的 Linux 配置

    系统版本 Ubuntu 18.04 一名老年弱智 OI 选手的 Linux 配置 文本编辑器: Sublime Text 中文补丁,关闭自动补全,自动联想,括号匹配,字号 15 编译器: g++ (然 ...

  9. HDU - 5412 CRB and Queries (整体二分)

    题目链接 动态区间第k小,但是这道题的话用主席树+树状数组套线段树的空间复杂度是O(nlog2n)会爆掉. 另一种替代的方法是用树状数组套平衡树,空间复杂度降到了O(nlogn),但我感觉平衡树是个挺 ...

  10. Maven实现直接部署Web项目到Tomcat7

    如题目,自动部署到Web服务器,直接上过程. 1.Tomcat7的用户及权限配置:在conf目录下,找到tomcat-users.xml,添加manager权限的用户. <role rolena ...