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版)
丰富的序列
37
match
语句中添加以下两行可以实现这种句法。
case ['define', [Symbol() as name, *parms], *body] if body:
env[name] = Procedure(parms, body, env)
我选择把这个
case
子句放在示例
2-12
中匹配其他
define
case
子句后面。在这个示例
中,
define
的不同情况以何种顺序放置其实无关紧要,因为没有哪个匹配对象能同时满足
所有模式:现有的
define
case
子句,第二个元素必须是
Symbol
实例;新增的快捷句法
用来定义函数,第二个元素必须是一个以
Symbol
实例开头的序列。
可以想象一下,如果按照示例
2-1
1
那样不使用模式匹配,为了支持新
define
句法需要多
少工作量。
match
语句做的事情比类
C
语言中的
switch
语句多出不少。
模式匹配是一种声明式编程风格,即描述你想匹配“什么”,而不是“如何”匹配,这样
写出的代码结构与数据结构是一致的,如表
2-2
所示。
2-2:一些 Scheme 句法形式和处理句法的
case
模式
Scheme
句法 序列模式
(quote exp) ['quote', exp]
(if test conseq alt) ['if', test, conseq, alt]
(lambda (parms...) body1 body2...) ['lambda', [*parms], *body] if body
(define name exp) ...
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