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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
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
選挙速報を多くのユーザーへ 届ける Live Activities 設計
hamayokokuririn
0
150
AIは賢い。でも実行環境は? CLIおじさんがAI時代に伝えたいこと ~ CLIおじさんがAI時代に伝えたいこと ~
curekoshimizu
1
250
iOSDC2026登壇資料.pdf
riofujimon
0
170
Agents on Rails - Rails at Scale 2026
irinanazarova
0
130
GraphRAGのKnowledge Graphを 直接!見る/View-GraphRAG's-KnowledgeGraph-directly!
tyumugi1113
1
370
JPUG勉強会 OSSデータベースの内部構造を理解しよう(第2回)
oga5
0
270
ハーネス設計入門 〜 基礎知識の整理から実務へのステップアップ 〜
kinopeee
8
5.8k
UPDATE をやめる — EF Core でマスタをバージョン管理する
panda728
PRO
0
310
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
260
スマートフォンでモールス信号を送受信する 〜スマートフォンのLEDとカメラで作る光通信の設計と実装〜
atsuki_seo
0
160
すこし踏み込む CancellationToken
htkym
2
700
Vue Fes Japan 2026 タイムテーブル徹底解説
448jp
1
460
Featured
See All Featured
A better future with KSS
kneath
240
18k
Prompt Engineering for Job Search
mfonobong
0
450
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
1
910
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Agile Leadership in an Agile Organization
kimpetersen
PRO
0
240
Testing 201, or: Great Expectations
jmmastey
46
8.3k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.6k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
1k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
50
10k
RailsConf 2023
tenderlove
30
1.6k
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
580
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
1
400
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