Skip to Content
流畅的Python(第2版)
book

流畅的Python(第2版)

by Luciano Ramalho
April 2023
Intermediate to advanced
769 pages
25h 16m
Chinese
Posts & Telecom Press
Content preview from 流畅的Python(第2版)
502
17
my_coro : Generator[YieldType, SendType, ReturnType]
从形式参数中的类型变量可以看出,
YieldType
ReturnType
可以协变,
SendType
可以逆
变。为了理解其中的原因,可以把
YieldType
ReturnType
看作“输出”类型,描述的都
是从协程对象(即用作协程的生成器对象)中获取的数据。
这两个类型参数可以协变的原因不难理解,因为预取产出浮点数的协程自然可以使用产出
整数的协程。这是
YieldType
参数可以协变的原因,同样也适用于可以协变的
ReturnType
参数。
根据
15.7.4
节的
“协变类型”提出的表示法,可协变的第
1
个和第
3
个参数可以使用
:>
符号表达,指向相同的方向。
float :> int
Generator[float, Any, float] :> Generator[int, Any, int]
YieldType
ReturnType
体现了
15.7.4
节的“型变经验法则”给出的第一条法则。
如果一个形式类型参数定义的是从对象中获取的数据类型,那么该形式类型参数
可能是协变的。
另一方面,
SendType
是“输入”参数,是协程对象
.send(value)
方法的
value
参数的类
型。如果客户代码需要向协程发送浮点数,那就不能使用
SendType
int
的协程,因为
float
不是
int
的子类型。也就是说,
float
int
不相容。但是,可以使用
SendType
complex
的协程,因为 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Python高级编程(第2版)

Python高级编程(第2版)

Posts & Telecom Press, Michał Jaworski, Tarek Ziadé
Python贝叶斯分析(第2版)

Python贝叶斯分析(第2版)

Posts & Telecom Press, Osvaldo Martin
高效能PYTHON程式設計

高效能PYTHON程式設計

Micha Gorelick, Ian Ozsvald

Publisher Resources

ISBN: 9787115612366