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
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
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
DSPy入門 Pythonで実現する自動プロンプト最適化 〜人手によるプロンプト調整からの卒業〜
seaturt1e
1
370
Lambda のコードストレージ容量に気をつけましょう
tattwan718
0
200
AIによる開発の民主化を支える コンテキスト管理のこれまでとこれから
mulyu
3
2.1k
AIに仕事を丸投げしたら、本当に楽になれるのか
dip_tech
PRO
0
170
Rubyと楽しいをつくる / Creating joy with Ruby
chobishiba
0
200
CSC307 Lecture 12
javiergs
PRO
0
450
CSC307 Lecture 11
javiergs
PRO
0
580
NOT A HOTEL - 建築や人と融合し、自由を創り出すソフトウェア
not_a_hokuts
2
480
CSC307 Lecture 09
javiergs
PRO
1
850
AWS Infrastructure as Code の新機能 2025 総まとめ 〜SA 4人による怒涛のデモ祭り〜
konokenj
10
2.7k
20260228_JAWS_Beginner_Kansai
takuyay0ne
4
360
LangChain4jとは一味違うLangChain4j-CDI
kazumura
1
120
Featured
See All Featured
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
100
Docker and Python
trallard
47
3.7k
The Pragmatic Product Professional
lauravandoore
37
7.2k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
470
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.1k
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
1
1.4k
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.1k
Typedesign – Prime Four
hannesfritz
42
3k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
220
Information Architects: The Missing Link in Design Systems
soysaucechin
0
810
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
250
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
1
140
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