它可以很简单地构造一个无限迭代器:

for i in iter(int, ):
print(i) #将无限打印出0

原来,如果iter有第二个参数,那么第一个参数必须是一个参数可以省略的可调用对象.int函数符合这种要求.

迭代什么时候停止呢?当int()==1的时候就停止.显然这是不可能的,所以就无限迭代了.

官方文档定义:

iter(object[, sentinel])

Return an iterator object. The first argument is interpreted very differently depending on the presence of the second argument. Without a second argument, object must be a collection object which supports the iteration protocol (the __iter__() method), or it must support the sequence protocol (the __getitem__() method with integer arguments starting at 0). If it does not support either of those protocols, TypeError is raised. If the second argument, sentinel, is given, then object must be a callable object. The iterator created in this case will call object with no arguments for each call to its __next__() method; if the value returned is equal to sentinel, StopIteration will be raised, otherwise the value will be returned.

iter 函数另类用法的更多相关文章

  1. python迭代器与iter()函数实例教程

    python迭代器与iter()函数实例教程 发布时间:2014-07-16编辑:脚本学堂 本文介绍了python迭代器与iter()函数的用法,Python 的迭代无缝地支持序列对象,而且它还允许程 ...

  2. python中的next()以及iter()函数

    我们首先要知道什么是可迭代的对象(可以用for循环的对象)Iterable: 一类:list,tuple,dict,set,str 二类:generator,包含生成器和带yield的generato ...

  3. Python 3 iter函数用法简述

    Python 3中关于iter(object[, sentinel)]方法有两个参数. 使用iter(object)这种形式比较常见. iter(object, sentinel)这种形式一般较少使用 ...

  4. python iter函数用法

    iter函数用法简述 Python 3中关于iter(object[, sentinel)]方法有两个参数. 使用iter(object)这种形式比较常见. iter(object, sentinel ...

  5. Python装饰器的另类用法

    之前有比较系统介绍过Python的装饰器(请查阅<详解Python装饰器>),本文算是一个补充.今天我们一起探讨一下装饰器的另类用法. 语法回顾 开始之前我们再将Python装饰器的语法回 ...

  6. enumerate next eval reload 内置函数的用法

    enumerate next eval reload 内置函数的用法 #enumerate() 函数用于将一个可遍历的数据对象(如列表.元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用 ...

  7. Python中的next()\iter()函数详解

    什么是可迭代的对象(Iterable,即可以用for循环的对象)和迭代器(Iterator) Iterable: 一类是:list.tuple.dict.set.str 二类是:generator(都 ...

  8. iter的特殊用法以及偏函数partial

    iter()的特殊用法 常规使用 iter(obj),会返现一个迭代器,如果 obj 不是可迭代对象,则会报错. 特殊用法(哨兵模式) iter(object[, sentinel]) sentine ...

  9. 有关日期的函数操作用法总结,to_date(),trunc(),add_months();

    相关知识链接: Oracle trunc()函数的用法 oracle add_months函数 Oracle日期格式转换,tochar(),todate() №2:取得当前日期是一个星期中的第几天,注 ...

随机推荐

  1. [POI2007]ZAP-Queries

    题目描述 Byteasar the Cryptographer works on breaking the code of BSA (Byteotian Security Agency). He ha ...

  2. ●SPOJ LCS2Longest Common Substring II

    题链: http://www.spoj.com/problems/LCS2/题解: 后缀自动机. 对第一个串建立后缀自动机, 然后把后面的每个串分别与该串的自动机去匹配,求出相应的数组val*[s]: ...

  3. ●UOJ 21 缩进优化

    题链: http://uoj.ac/problem/21 题解: ...技巧题吧 先看看题目让求什么: 令$F(x)=\sum_{i=1}^{n}(\lfloor a[i]/x \rfloor +a[ ...

  4. 细胞(cell) 矩阵快速幂

    题目描述小 X 在上完生物课后对细胞的分裂产生了浓厚的兴趣.于是他决定做实验并观察细胞分裂的规律.他选取了一种特别的细胞,每天每个该细胞可以分裂出 x 1 个新的细胞.小 X 决定第 i 天向培养皿中 ...

  5. bzoj4665小w的喜糖 dp+容斥

    4665: 小w的喜糖 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 120  Solved: 72[Submit][Status][Discuss] ...

  6. oracle 分页查询数据重复问题

    最近在做项目的时候发现一个问题,oracle 在查询分页数据的时候,有几条数据重复查询了,并且有几条数据在分页的时候消失了.百度了一下发现,ORACLE 在查询数据的时候返回的行不是固定的,他只是按照 ...

  7. 一起来Fit TDMA over WiFi(1)

    1 概述 WiFI TDMA领域,2009年Sam  Leffler在<TDMA for Long Distance Wireless Networks>首次系统提出了TDMA技术方案,并 ...

  8. ANI功能分析

    1 ANI ANI(Adapt Noise Immunity)就是基于CCK错包率,和/或CCK错包率,自动调整抗扰等级,从而提高或降低灵敏度,达到提高整体性能的目标. 2 关键常量 firstep_ ...

  9. Linux 软件安装到 /usr,/usr/local/ 还是 /opt 目录区别

    Linux 的软件安装目录是也是有讲究的,理解这一点,在对系统管理是有益的 /usr:系统级的目录,可以理解为C:/Windows/, /usr/lib理解为C:/Windows/System32./ ...

  10. 浅谈@RequestMapping @ResponseBody 和 @RequestBody 注解的用法与区别

    浅谈@RequestMapping @ResponseBody 和 @RequestBody 注解的用法与区别 Spring 2.5 版本新增了注解功能, 通过注解,代码编写简化了很多:但熟悉注解的使 ...