Python中for else注意事项
假设有如下代码:
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注意事项的更多相关文章
- python中除法的注意事项
使用python数据处理,代码如下: import numpy as np fs = 5 ts = np.arange(-1,1+1/fs,1/fs) 发现了这样一个错误: Traceback (mo ...
- Navicat的使用与python中使用MySQL的基本方法
Navicat的使用与python中使用MySQL的基本方法 Navicat的下载及安装 下载地址 http://www.navicat.com.cn/download/navicat-premium ...
- python中的join.set ,copy以及删除注意事项:
1 ,join : 将对象以字符串的方式拼接成一个整体 for E : li = ["李白", "是", "诗仙"] s = & ...
- python中的注意事项
.python 中的 and 从左到右计算表达式,若所有值均为真,则返回最后一个值,若存在假,返回第一个假值: or 也是从左到有计算表达式,返回第一个为真的值: 其中数字 0 是假,其他都是真: 字 ...
- 【python】继承时注意事项
1. __init__ 注意事项 如果父类有__init__函数,子类没有,则子类自动调用父类__init__函数 如果父类有__init__函数,子类也有,则子类必须主动调用父类__init__函数 ...
- [转载]python中multiprocessing.pool函数介绍
原文地址:http://blog.sina.com.cn/s/blog_5fa432b40101kwpi.html 作者:龙峰 摘自:http://hi.baidu.com/xjtukanif/blo ...
- 【转载】Python中如何高效实现两个字典合并,三种方法比较。
本文转载自:http://www.pythoner.com/13.html Python中将两个字典进行合并操作,是一个比较常见的问题.本文将介绍几种实现两个字典合并的方案,并对其进行比较. 对于这个 ...
- python中multiprocessing.pool函数介绍_正在拉磨_新浪博客
python中multiprocessing.pool函数介绍_正在拉磨_新浪博客 python中multiprocessing.pool函数介绍 (2010-06-10 03:46:5 ...
- 如何导入python中的模块
作为一名新手Python程序员,你首先需要学习的内容之一就是如何导入模块或包.但是我注意到,那些许多年来不时使用Python的人并不是都知道Python的导入机制其实非常灵活.在本文中,我们将探讨以下 ...
随机推荐
- 移动端rem设置,自动更改html<font-size>
<script> (function (doc, win) { var docEl = doc.documentElement, resizeEvt = 'orientationchang ...
- Javasript 内置函数
var str = 'AAAA';var aTest= new Array(); //['ff'[,'er']] \ new Array(10); \ new Array('ff','fee');va ...
- const、define与sizeof
一.const的用途 1.定义const常量 2.可以修饰函数的形参,返回值,以及函数体.被const修饰的内容可以受到强制保护,防止被意外修改,提高程序健壮性. const 返回值 函数返回值为 c ...
- nodejs--vue
nodejs--vue 基础知识认识: 前端工程化 最近才兴起,nodejs(包的管理更加方便),webpack 数据双向绑定 mvm 数据驱动vue,vue改变数据 组件化开发 vue 中的常见 概 ...
- const指针和指向const的指针
int *const p=&a; 这是const指针,这种指针必须在定义时就给出它所指向的地址,否则会error:uninitialized const 'p'.const指针的指针本身是co ...
- 在asp.net中使JQuery的Ajax用总结
自从有了JQuery,Ajax的使用变的越来越方便了,但是使用中还是会或多或少的出现一些让人短时间内痛苦的问题.本文暂时总结一些在使用JQuery Ajax中应该注意的问题,如有不恰当或者不完善的地方 ...
- BZOJ4520:[CQOI2016]K远点对
浅谈\(K-D\) \(Tree\):https://www.cnblogs.com/AKMer/p/10387266.html 题目传送门:https://lydsy.com/JudgeOnline ...
- PHP7卓越性能背后的原理有哪些?
作者:韩天峰链接:http://www.zhihu.com/question/38148900/answer/75115687来源:知乎 PHP7在运行原理上与PHP5相比并没有变化,这与hhvm不同 ...
- 聊聊 SQL Joins
SQL 中的 Join 有以下几种类型: 1.Cross Join 交叉连接,没有条件筛选,返回笛卡尔积. 如果以 ,(逗号)分隔表名进行查询如 select * from tbl_name1, tb ...
- Unreal Engine 4 Radiant UI 入门教程(一)制作Radiant HUD
请注意:本篇教程的编号是从零开始的,如果没有看第零篇教程,请前往学习. 本教程介绍如何制作Radiant HUD,这是指将网页元素直接加入到HUD中,效果为: 对应的网页元素为: 第一步: 将之前下载 ...