首页 > 编程语言 > 详细

XiaoKL学Python(E)Generator Expressions

时间:2015-04-23 12:57:20      阅读:264      评论:0      收藏:0      [点我收藏+]

在 阅读 https://github.com/vitonzhang/objc_dep 中的 objc_dep.py 时遇到:

1 objc_files = (f for f in files if f.endswith(ext))

在Ref[1] PEP中,这种语法称为 Generator Expressions。

例如:

1 g = (x**2 for x in range(10))
2 print g.next()

 

等价于:

1 def __gen(exp):
2     for x in exp:
3         yield x**2
4 g = __gen(iter(range(10)))
5 print g.next()

 

 


 

Reference

1. PEP 289 - Generator Expressions

https://www.python.org/dev/peps/pep-0289/

XiaoKL学Python(E)Generator Expressions

原文:http://www.cnblogs.com/cwgk/p/4449832.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!