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版)
丰富的序列
57
什么始终应该通过
2
i
< 13
表达,除此之外也有其他约定写法,可能合理,但是均被否
定,因为不能让每一个用户都各行其是。虽然这篇备忘录的标题指的是基于零的索引,但
是文内讨论的其实是为什么
'ABCDE'[1:3]
得到的是
'BC'
而不是
'BCD'
,以及为什么通过
range(2, 13)
生成
2, 3, 4,
, 12
最符合常理。顺便说一下,这篇备忘是手写笔记,不过字
迹清晰可辨。甚至还有人根据这篇笔记设计了一种字体。
杂谈
元组的本质
2012
年,我在
PyCon US
上展示了一张关于
ABC
语言的海报。创造
Python
之前,
Guido van Rossum
曾开发过
ABC
语言的解释器,所以他过来看了我的海报。交谈中
我们讲到了
ABC
语言的
compound
类型,这显然是
Python
元组的前身。
compound
类型还支持并行赋值,也可用作字典(或
ABC
语言中的
Table
类型)的组合键。然
而,
compound
类型不是序列,不可迭代,不能通过索引获取字段,更别提切片了。
compound
类型只能作为一个整体处理,或者通过并行赋值提取各个字段,仅此而已。
我跟
Guido
讲,这些限制凸显了
compound
类型的主要用途,即没有具名字段的记录。
他回应道:“为了让元组显现序列行为,可是费了一番功夫。”
这是一种务实的做法,正是因为
Python
更务实,所以才比
ABC
语言成功。从语言实
现者的角度来看,让元组表现为序列的成本很低。这样做虽然把记录这种用途弱化了,
但得到了一种不可变列表——把元组类型命名为 ...
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