Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Python Generators
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
laike9m
December 31, 2013
Programming
1
120
Python Generators
讲述Python生成器的知识
laike9m
December 31, 2013
Tweet
Share
More Decks by laike9m
See All by laike9m
Python First Class_v1.1
laike9m
0
130
Python HTTP
laike9m
0
130
ChinaUnicom 模拟登陆
laike9m
0
120
Python First Class
laike9m
0
130
Other Decks in Programming
See All in Programming
Java 21/25 Virtual Threads 소개
debop
0
280
Feature Toggle は捨てやすく使おう
gennei
0
360
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
1.1k
KagglerがMixSeekを触ってみた
morim
0
330
Rethinking API Platform Filters
vinceamstoutz
0
980
モダンOBSプラグイン開発
umireon
0
180
Nuxt Server Components
wattanx
0
130
ポーリング処理廃止によるイベント駆動アーキテクチャへの移行
seitarof
3
1.3k
Migration to Signals, Signal Forms, Resource API, and NgRx Signal Store @Angular Days 03/2026 Munich
manfredsteyer
PRO
0
160
AI時代の脳疲弊と向き合う ~言語学としてのPHP~
sakuraikotone
1
1.6k
PHP 7.4でもOpenTelemetryゼロコード計装がしたい! / PHPerKaigi 2026
arthur1
1
420
Goの型安全性で実現する複数プロダクトの権限管理
ishikawa_pro
2
1.4k
Featured
See All Featured
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
100
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
118
110k
Odyssey Design
rkendrick25
PRO
2
560
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
Into the Great Unknown - MozCon
thekraken
40
2.3k
Art, The Web, and Tiny UX
lynnandtonic
304
21k
Why You Should Never Use an ORM
jnunemaker
PRO
61
9.8k
GitHub's CSS Performance
jonrohan
1032
470k
The SEO identity crisis: Don't let AI make you average
varn
0
420
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
190
Testing 201, or: Great Expectations
jmmastey
46
8.1k
Transcript
Python Generators by laike9m
[email protected]
https://github.com/laike9m
You don’t know what you don’t know 听说过,但不理解>> 没听说过
None
None
None
>>> def gen_ab(): ... print('starting...') ... yield 'A' ... print('here
comes B:') ... yield 'B' ... print('the end.') g = gen_ab(), 停在这里 第一次调用next(), 停在这里 第二次调用next(), 停在这里 生成器函数可以看成一串事件,yield暂停执行,next恢复执行 “yield 是 具有 暂停功能 的 return”
斐波那契数列: 0,1,1,2,3,5,8,11,19,... 前两个数是0,1,后一个数是前两个数之和 斐波那契数列生成器
for 循环 每次都会调用next() 更深入的讨论:interator(迭代器) 每次都把返回的a输出 注意yield是“具有暂停功能的return” return a ( =
yield a ) fib_number = a 然后打印出来
• 循环就是一个事件流,只不过里面 包含了一些条件判断 def fib(max): a, b = 0, 1
while a < max: yield a a, b = b, a+b 等价于 def fib(max): a, b = 0, 1 if a < max: yield a a, b = b, a+b if a < max: yield a a, b = b, a+b if a < max: yield a a, b = b, a+b ...
• 一起写一个斐波那契生成器 • enumerate 函数
list() : 调用next()直到不能调用为止,并且把返回值存入列表,实质就是列表解析 [i for i in enumerate(seasons)]
None
g.next()是Python2.X 的写法,对应 Python3.X中的next(g)
• 有yield的函数:生成器函数 • yied是带暂停功能的return • for, list 本质上是在调用next(obj) • 把生成器函数看成事件流,yield暂停,next继续执行
• 生成器函数内部通常包含循环,循环也是事件流 • 把列表解析的中括号换成小括号就是生成器表达式
• 快 • 内存占用小 需要的时候才会产生 • 语义上的含义 一次性使用 • 保存函数的当前执行环境,包括所有局部变量等
http://stackoverflow.com/questions/231767/the-python-yield-keyword-explained
Q&A