python iter函数特殊用法
来源:互联网 发布:阿拉丁控股集团 知乎 编辑:程序博客网 时间:2024/06/05 01:05
Python 中迭代对象时会调用 iter(x)。
iter 函数还有一个鲜为人知的用法: 传入两个参数, 使用常规的函数或任何可调用的对象创建迭代器。
第一个参数必须是可调用的对象,用于不断调用(没有参数),产出各个值。
第二个值是哨符,这是个标记值, 当可调用的对象返回这个值时, 触发迭代器抛出 StopIteration 异常,而不产出哨符。
from random import randint>>> def random_num():... return randint(1, 6)... >>> num = iter(random_num, 1)>>> num<callable-iterator object at 0x263aad0>>>> for i in num:... print(i)... 446664这里iter函数返回一个callable_iterator对象。1是哨符,对象返回这个值时触发迭代器抛出 StopIteration 异常。循环结束 与常规的迭代器一样,示例中的num对象一旦耗尽就没用了。
阅读全文
0 0
- python iter函数特殊用法
- Python中iter函数用法简述
- python iter()用法
- python iter( )函数
- Python iter函数详解
- python: iter、next函数
- python iter( )函数
- python中迭代器和iter()函数
- python迭代器和iter()函数
- python中迭代器和iter()函数
- Python迭代器和iter()函数
- python 特殊函数的用法
- python中的iter()函数与next()函数
- Python iter
- 关于pytho中函数iter的用法详解
- Python标准库:内置函数iter(object[, sentinel])
- Python标准库:内置函数iter(object[, sentinel])
- python map iter
- Io流注册登录项目,注册的用户可以在电脑上保存下来,方便下次登录
- Hihocoder Offer28 习题总结
- Windows同时安装Python2、Python3兼容运行
- PL/SQL 学习分享(续)
- 用 LoadLibraryEx 失败
- python iter函数特殊用法
- 虚拟机VMware中Ubuntu编译安装lnmp时,如何复制windows中的内容,粘贴到Ubuntu中!
- LeetCode50_Pow(x,n)
- java 算法之冒泡排序
- Java---- 静态内部类与非静态内部类的区别
- 一个简单的HttpServlet
- Linux 防火墙开放端口ptables
- MapReduce2.0原理及基本架构
- 迅雷前端一面