Skip to content

Commit a0195c0

Browse files
committed
9.19小节完成
1 parent 0f32ca1 commit a0195c0

File tree

1 file changed

+26
-39
lines changed

1 file changed

+26
-39
lines changed

source/c09/p19_initializing_class_members_at_definition_time.rst

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,16 @@
55
----------
66
问题
77
----------
8-
You want to initialize parts of a class definition once at the time a class is defined, not
9-
when instances are created.
8+
你想在类被定义的时候就初始化一部分类的成员,而不是要等到实例被创建后。
109

1110

1211
----------
1312
解决方案
1413
----------
15-
Performing initialization or setup actions at the time of class definition is a classic use
16-
of metaclasses. Essentially, a metaclass is triggered at the point of a definition, at which
17-
point you can perform additional steps.
14+
在类定义时就执行初始化或设置操作是元类的一个典型应用场景。本质上讲,一个元类会在定义时被触发,
15+
这时候你可以执行一些额外的操作。
1816

19-
20-
Here is an example that uses this idea to create classes similar to named tuples from the
21-
collections module:
17+
下面是一个例子,利用这个思路来创建类似于 ``collections`` 模块中的命名元组的类:
2218

2319
.. code-block:: python
2420
@@ -37,7 +33,7 @@ collections module:
3733
raise ValueError('{} arguments required'.format(len(cls._fields)))
3834
return super().__new__(cls,args)
3935
40-
This code allows simple tuple-based data structures to be defined, like this:
36+
这段代码可以用来定义简单的基于元组的数据结构,如下所示:
4137

4238
.. code-block:: python
4339
@@ -47,7 +43,7 @@ This code allows simple tuple-based data structures to be defined, like this:
4743
class Point(StructTuple):
4844
_fields = ['x', 'y']
4945
50-
Here’s how they work:
46+
下面演示它如何工作:
5147

5248
.. code-block:: python
5349
@@ -72,42 +68,33 @@ Here’s how they work:
7268
讨论
7369
----------
7470

75-
In this recipe, the StructTupleMeta class takes the listing of attribute names in the
76-
_fields class attribute and turns them into property methods that access a particular
77-
tuple slot. The operator.itemgetter() function creates an accessor function and the
78-
property() function turns it into a property.
71+
这一小节中,类 ``StructTupleMeta`` 获取到类属性 ``_fields`` 中的属性名字列表,
72+
然后将它们转换成相应的可访问特定元组槽的方法。函数 ``operator.itemgetter()`` 创建一个访问器函数,
73+
然后 ``property()`` 函数将其转换成一个属性。
7974

80-
The trickiest part of this recipe is knowing when the different initialization steps occur.
81-
The __init__() method in StructTupleMeta is only called once for each class that is
82-
defined. The cls argument is the class that has just been defined. Essentially, the code
83-
is using the _fields class variable to take the newly defined class and add some new
84-
parts to it.
75+
本节最难懂的部分是知道不同的初始化步骤是什么时候发生的。
76+
``StructTupleMeta`` 中的 ``__init__()`` 方法只在每个类被定义时被调用一次。
77+
``cls`` 参数就是那个被定义的类。实际上,上述代码使用了 ``_fields`` 类变量来保存新的被定义的类,
78+
然后给它再添加一点新的东西。
8579

86-
The StructTuple class serves as a common base class for users to inherit from. The
87-
__new__() method in that class is responsible for making new instances. The use of
88-
__new__() here is a bit unusual, but is partly related to the fact that we’re modifying the
89-
calling signature of tuples so that we can create instances with code that uses a normallooking
90-
calling convention like this:
80+
``StructTuple`` 类作为一个普通的基类,供其他使用者来继承。
81+
这个类中的 ``__new__()`` 方法用来构造新的实例。
82+
这里使用 ``__new__()`` 并不是很常见,主要是因为我们要修改元组的调用签名,
83+
使得我们可以像普通的实例调用那样创建实例。就像下面这样:
9184

9285
.. code-block:: python
9386
9487
s = Stock('ACME', 50, 91.1) # OK
9588
s = Stock(('ACME', 50, 91.1)) # Error
9689
97-
Unlike __init__(), the __new__() method gets triggered before an instance is created.
98-
Since tuples are immutable, it’s not possible to make any changes to them once they
99-
have been created. An __init__() function gets triggered too late in the instance creation
100-
process to do what we want. That’s why __new__() has been defined.
101-
102-
103-
Although this is a short recipe, careful study will reward the reader with a deep insight
104-
about how Python classes are defined, how instances are created, and the points at which
105-
different methods of metaclasses and classes are invoked.
106-
90+
跟 ``__init__()`` 不同的是,``__new__()`` 方法在实例被创建之前被触发。
91+
由于元组是不可修改的,所以一旦它们被创建了就不可能对它做任何改变。而 ``__init__()`` 会在实例创建的最后被触发,
92+
这样的话我们就可以做我们想做的了。这也是为什么 ``__new__()`` 方法已经被定义了。
10793

108-
`PEP 422 <http://www.python.org/dev/peps/pep-0422>`_ may provide an alternative
109-
means for performing the task described in this recipe.
110-
However, as of this writing, it has not been adopted or accepted. Nevertheless,
111-
it might be worth a look in case you’re working with a version of Python newer than
112-
Python 3.3.
94+
尽管本机很短,还是需要你能仔细研读,深入思考Python类是如何被定义的,实例是如何被创建的,
95+
还有就是元类和类的各个不同的方法究竟在什么时候被调用。
11396

97+
`PEP 422 <http://www.python.org/dev/peps/pep-0422>`_
98+
提供了一个解决本节问题的另外一种方法。
99+
但是,截止到我写这本书的时候,它还没被采纳和接受。
100+
尽管如此,如果你使用的是Python 3.3或更高的版本,那么还是值得去看一下的。

0 commit comments

Comments
 (0)