Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Python Generators
Search
laike9m
December 31, 2013
Programming
1
110
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
110
Python First Class
laike9m
0
130
Other Decks in Programming
See All in Programming
JETLS.jl ─ A New Language Server for Julia
abap34
2
450
マスタデータ問題、マイクロサービスでどう解くか
kts
0
120
Canon EOS R50 V と R5 Mark II 購入でみえてきた最近のデジイチ VR180 事情、そして VR180 静止画に活路を見出すまで
karad
0
140
AIエージェントの設計で注意するべきポイント6選
har1101
5
2.3k
AIコーディングエージェント(Gemini)
kondai24
0
270
AI 駆動開発ライフサイクル(AI-DLC):ソフトウェアエンジニアリングの再構築 / AI-DLC Introduction
kanamasa
11
3.8k
生成AIを利用するだけでなく、投資できる組織へ
pospome
2
410
令和最新版Android Studioで化石デバイス向けアプリを作る
arkw
0
440
20251212 AI 時代的 Legacy Code 營救術 2025 WebConf
mouson
0
210
AIコーディングエージェント(Manus)
kondai24
0
210
脳の「省エネモード」をデバッグする ~System 1(直感)と System 2(論理)の切り替え~
panda728
PRO
0
120
LLM Çağında Backend Olmak: 10 Milyon Prompt'u Milisaniyede Sorgulamak
selcukusta
0
130
Featured
See All Featured
Believing is Seeing
oripsolob
0
15
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
9.1k
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
57
37k
Site-Speed That Sticks
csswizardry
13
1k
How to build a perfect <img>
jonoalderson
0
4.7k
Bootstrapping a Software Product
garrettdimon
PRO
307
120k
The Cost Of JavaScript in 2023
addyosmani
55
9.4k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
5.8k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.6k
More Than Pixels: Becoming A User Experience Designer
marktimemedia
2
260
Building Adaptive Systems
keathley
44
2.9k
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
69
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