未验证 提交 c58ca499 编写于 作者: 飞龙 提交者: GitHub

Update 2.2.md

上级 86ab6dce
......@@ -100,7 +100,13 @@ True
Python 中的元组(以及多数其它编程语言中的序列)下标都以 0 开始,也就是说,下标 0 表示第一个元素,下标 1 表示第二个元素,以此类推。我们对这个下标惯例的直觉是,下标表示一个元素距离元组开头有多远。
与元素选择操作等价的函数叫做`getitem`,它也使用下标以 0 开始的位置来在元组中选择元素。
与元素选择操作等价的函数叫做`__getitem__`,它也使用位置在元组中选择元素,位置的下标以 0 开始。
```py
>>> from operator import getitem
>>> getitem(pair, 0)
1
```
元素是原始类型,也就是说 Python 的内建运算符可以操作它们。我们不久之后再来看元素的完整特性。现在,我们只对元组如何作为胶水来实现抽象数据类型感兴趣。
......@@ -110,9 +116,9 @@ Python 中的元组(以及多数其它编程语言中的序列)下标都以
>>> def make_rat(n, d):
return (n, d)
>>> def numer(x):
return x.__getitem__(0)
return getitem(x, 0)
>>> def denom(x):
return x.__getitem__(1)
return getitem(x, 1)
```
用于打印有理数的函数完成了我们对抽象数据结构的实现。
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册