Skip to content

Commit e87ec50

Browse files
author
Xudong
authored
Update 2.7.md with completing missing translations
There is a piece of important code and the accompanying descriptive text is not translated. This commit fixed is.
1 parent bc68dc3 commit e87ec50

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

2.7.md

+10
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,16 @@ ComplexMA(1.0, 3.141592653589793)
268268
return add.implementations[types](z1, z2)
269269
```
270270

271+
add函数的定义本身没有任何功能;它完全地依赖于一个叫做add.implementations的字典去实现泛用加法。我们可以构建如下的字典。
272+
273+
```py
274+
>>> add.implementations = {}
275+
>>> add.implementations[('com', 'com')] = add_complex
276+
>>> add.implementations[('com', 'rat')] = add_complex_and_rational
277+
>>> add.implementations[('rat', 'com')] = lambda x, y: add_complex_and_rational(y, x)
278+
>>> add.implementations[('rat', 'rat')] = add_rational
279+
```
280+
271281
这个基于字典的分发方式是递增的,因为`add.implementations``type_tag.tags`总是可以扩展。任何新的数值类型可以将自己“安装”到现存的系统中,通过向这些字典添加新的条目。
272282

273283
当我们向系统引入一些复杂性时,我们现在拥有了泛用、可扩展的`add`函数,可以处理混合类型。

0 commit comments

Comments
 (0)