Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Python Generators
Search
laike9m
December 31, 2013
Programming
130
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Python Generators
讲述Python生成器的知识
laike9m
December 31, 2013
More Decks by laike9m
See All by laike9m
Python First Class_v1.1
laike9m
0
140
Python HTTP
laike9m
0
140
ChinaUnicom 模拟登陆
laike9m
0
130
Python First Class
laike9m
0
150
Other Decks in Programming
See All in Programming
AWSに止められる覚悟してますか?
morizo_1984
1
260
世界の中心で、AI(App Intents)をさけぶ ー App Intents中心設計の実践ガイド
touyou
0
660
個人開発基盤をまるごとCloudflareに引っ越して爆速で総合的体験を向上させた話
tinykitten
0
200
Intent as Code
shoppingjaws
6
1.1k
Everything will be SERVERLESS — 信じて運用した10年の経験値 / Everything Will be Serverless — Lessons Learned from 10 Years of Operational Experience
seike460
PRO
1
490
[GoCon2026] When Goroutines Are Not Enough: Runtime Locality in High-Throughput Go
takehaya
6
2.5k
Snowflakeで業務アプリを作ろう。 Snowflakeのアプリ機能解説&実践ガイド
ayumu_yamaguchi
2
300
SONY CISC-NEWS NWS-1750 + NWB-225 フレームバッファの NetBSD/news68k ドライバ実装 / OSC2026Hiroshima
tsutsui
0
150
スマートフォンでモールス信号を送受信する 〜スマートフォンのLEDとカメラで作る光通信の設計と実装〜
atsuki_seo
0
180
動作中のプログラムの中身をリアルタイムに覗く / Realtime Debugger for CSharp with Roslyn
prota
1
1.2k
iOS 27でニュースアプリはどう変わる!? 〜日経電子版の新機能対応と、開発事例から〜
lynnswap
5
12k
Starting & Sustaining Code-Based E2E Testing for Non-Coding QA Teams( #jasstniigata )
teyamagu
PRO
1
520
Featured
See All Featured
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.8k
Marketing to machines
jonoalderson
1
5.8k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
430
My Coaching Mixtape
mlcsv
0
320
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
510
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.5k
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
1
770
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
2.2k
Visualization
eitanlees
153
17k
Joys of Absence: A Defence of Solitary Play
codingconduct
1
530
エンジニアに許された特別な時間の終わり
watany
109
250k
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