@@ -489,6 +489,8 @@ msgid ""
489489"it's created. Initializers always accept positional and keyword arguments, "
490490"and they should return either ``0`` on success or ``-1`` on error."
491491msgstr ""
492+ ":c:member:`~PyTypeObject.tp_init` 槽位在 Python 中暴露为 :meth:`~object.__init__` "
493+ "方法。 它被用来在创建对象后对其进行初始化。 初始化器总是接受位置和关键字参数,它们应当在成功时返回 ``0`` 而在出错时返回 ``-1``。"
492494
493495#: ../../extending/newtypes_tutorial.rst:391
494496msgid ""
@@ -500,6 +502,9 @@ msgid ""
500502"new attribute values. We might be tempted, for example to assign the "
501503"``first`` member like this::"
502504msgstr ""
505+ "不同于 ``tp_new`` 处理句柄,``tp_init`` 不保证一定会被调用 (例如,在默认情况下 :mod:`pickle` "
506+ "模块不会在未解封的实例上调用 :meth:`~object.__init__`)。 它还可能被多次调用。 任何人都可以在我们的对象上调用 "
507+ ":meth:`!__init__` 方法。 因此,我们在为属性赋新值时必须格外小心。 例如像这样给 ``first`` 成员赋值::"
503508
504509#: ../../extending/newtypes_tutorial.rst:405
505510msgid ""
@@ -576,7 +581,7 @@ msgstr ""
576581msgid ""
577582"We define a single method, :meth:`!Custom.name()`, that outputs the objects "
578583"name as the concatenation of the first and last names. ::"
579- msgstr ""
584+ msgstr "我们定义了一个单独的方法,:meth:`!Custom.name()`,它将对象名称输出为 first 和 last 的拼接。 :: "
580585
581586#: ../../extending/newtypes_tutorial.rst:469
582587msgid ""
@@ -587,6 +592,9 @@ msgid ""
587592" to accept a positional argument tuple or keyword argument dictionary. This "
588593"method is equivalent to the Python method:"
589594msgstr ""
595+ "该方法以的实现形式是一个接受 :class:`!Custom` (或:class:`!Custom` 的子类) 实例作为第一个参数的 C 函数。 "
596+ "方法总是接受一个实例作为第一个参数。 方法往往也接受位置和关键字参数,但在本例中我们未接受任何参数也不需要接受位置参数元组或关键字参数字典。 "
597+ "该方法等价于以下 Python 方法:"
590598
591599#: ../../extending/newtypes_tutorial.rst:481
592600msgid ""
@@ -596,6 +604,9 @@ msgid ""
596604"deletion of these attributes and to restrict the attribute values to be "
597605"strings. We'll see how to do that in the next section."
598606msgstr ""
607+ "请注意我们必须检查 :attr:`!first` 和 :attr:`!last` 成员是否可能为 ``NULL``。 "
608+ "这是因为它们可以被删除,在此情况下它们会被设为 ``NULL``。 更好的做法是防止删除这些属性并将属性的值限制为字符串。 "
609+ "我们将在下一节了解如何做到这一点。"
599610
600611#: ../../extending/newtypes_tutorial.rst:487
601612msgid ""
@@ -629,19 +640,21 @@ msgid ""
629640"module name in the :c:type:`PyModuleDef` struct, and update the full class "
630641"name in the :c:type:`PyTypeObject` struct."
631642msgstr ""
643+ "我们将 :c:func:`!PyInit_custom` 重命名为 :c:func:`!PyInit_custom2`,更新 "
644+ ":c:type:`PyModuleDef` 结构体中的模块名称,并更新 :c:type:`PyTypeObject` 结构体中的完整类名。"
632645
633646#: ../../extending/newtypes_tutorial.rst:515
634647msgid ""
635648"Finally, we update our :file:`setup.py` file to include the new module,"
636- msgstr ""
649+ msgstr "最后,我们更新 :file:`setup.py` 文件来包括新的模块, "
637650
638651#: ../../extending/newtypes_tutorial.rst:525
639652msgid "and then we re-install so that we can ``import custom2``:"
640- msgstr ""
653+ msgstr "然后我们重新安装以便能够 ``import custom2``: "
641654
642655#: ../../extending/newtypes_tutorial.rst:532
643656msgid "Providing finer control over data attributes"
644- msgstr "提供对于数据属性的更精细控制Providing finer control over data attributes "
657+ msgstr "提供对于数据属性的更精细控制 "
645658
646659#: ../../extending/newtypes_tutorial.rst:534
647660msgid ""
@@ -651,13 +664,18 @@ msgid ""
651664":attr:`!last` could be set to non-string values or even deleted. We want to "
652665"make sure that these attributes always contain strings."
653666msgstr ""
667+ "在本节中,我们将对 :class:`!Custom` 示例中 :attr:`!first` 和 :attr:`!last` 属性的设置进行更精细的控制。"
668+ " 在我们上一版本的模块中,实例变量 :attr:`!first` 和 :attr:`!last` 可以被设为非字符串值甚至被删除。 "
669+ "我们希望确保这些属性始终包含字符串。"
654670
655671#: ../../extending/newtypes_tutorial.rst:543
656672msgid ""
657673"To provide greater control, over the :attr:`!first` and :attr:`!last` "
658674"attributes, we'll use custom getter and setter functions. Here are the "
659675"functions for getting and setting the :attr:`!first` attribute::"
660676msgstr ""
677+ "为了更好地控制 :attr:`!first` 和 :attr:`!last` 属性,我们将使用自定义的读取器和设置器函数。 以下就是用于读取和设置 "
678+ ":attr:`!first` 属性的函数::"
661679
662680#: ../../extending/newtypes_tutorial.rst:574
663681msgid ""
@@ -668,6 +686,8 @@ msgid ""
668686"of getter and setter functions that decide the attribute to get or set based"
669687" on data in the closure.)"
670688msgstr ""
689+ "读取器函数接受一个 :class:`!Custom` 对象和一个“闭包”,后者是一个空指针。 在本例中,该闭包将被忽略。 (闭包支持将定义数据传递给 "
690+ "读取器和设置器的高级用法。 例如,这可以被用来允许一组获取器和设置器函数根据闭包中的数据来决定要读取或设置的属性)。"
671691
672692#: ../../extending/newtypes_tutorial.rst:580
673693msgid ""
@@ -676,6 +696,8 @@ msgid ""
676696" is being deleted. In our setter, we raise an error if the attribute is "
677697"deleted or if its new value is not a string."
678698msgstr ""
699+ "设置器函数接受传入 :class:`!Custom` 对象、新值和闭包。 新值可能为 ``NULL``,在这种情况下属性将被删除。 "
700+ "在我们的设置器中,如果属性被删除或者如果其新值不是字符串则会引发一个错误。"
679701
680702#: ../../extending/newtypes_tutorial.rst:585
681703msgid "We create an array of :c:type:`PyGetSetDef` structures::"
@@ -756,6 +778,9 @@ msgid ""
756778":class:`!Custom`, and subclasses may add arbitrary attributes. For any of "
757779"those two reasons, :class:`!Custom` objects can participate in cycles:"
758780msgstr ""
781+ "在 :class:`!Custom` 示例的第二个版本中,我们允许任意类型的对象存储到 :attr:`!first` 或 :attr:`!last` "
782+ "属性中 [#]_。 此外,在第二和第三个版本中,我们还允许子类化 :class:`!Custom`,并且子类可以添加任意属性。 "
783+ "出于这两个原因中的任何一个,:class:`!Custom` 对象都可以加入循环:"
759784
760785#: ../../extending/newtypes_tutorial.rst:683
761786msgid ""
@@ -764,6 +789,8 @@ msgid ""
764789"type needs to fill two additional slots and to enable a flag that enables "
765790"these slots:"
766791msgstr ""
792+ "要允许一个加入引用循环的 :class:`!Custom` 实例能被循环 GC 正确检测和收集,我们的 :class:`!Custom` "
793+ "类型需要填充两个额外的槽位并增加一个旗标来启用这些槽位:"
767794
768795#: ../../extending/newtypes_tutorial.rst:690
769796msgid ""
@@ -779,6 +806,8 @@ msgid ""
779806"argument *arg* passed to the traversal method. It returns an integer value "
780807"that must be returned if it is non-zero."
781808msgstr ""
809+ "对于每个可以加入循环的子对象,我们都需要调用 :c:func:`!visit` 函数,它会被传递给遍历方法。 :c:func:`!visit` "
810+ "函数接受该子对象和传递给遍历方法的额外参数 *arg* 作为其参数。 它返回一个在其为非零值时必须被返回的整数值。"
782811
783812#: ../../extending/newtypes_tutorial.rst:716
784813msgid ""
@@ -878,13 +907,15 @@ msgid ""
878907"with regular lists, but will have an additional :meth:`!increment` method "
879908"that increases an internal counter:"
880909msgstr ""
910+ "在本例中我们将创建一个继承自内置 :class:`list` 类型的 :class:`!SubList` 类型。 "
911+ "这个新类型将完全兼容常规列表,但将拥有一个额外的 :meth:`!increment` 方法用于递增内部计数器的值:"
881912
882913#: ../../extending/newtypes_tutorial.rst:814
883914msgid ""
884915"As you can see, the source code closely resembles the :class:`!Custom` "
885916"examples in previous sections. We will break down the main differences "
886917"between them. ::"
887- msgstr ""
918+ msgstr "如你所见,此源代码与之前几节中的 :class:`!Custom` 示例非常相似。 我们将逐一分析它们之间的主要区别。 :: "
888919
889920#: ../../extending/newtypes_tutorial.rst:822
890921msgid ""
@@ -900,12 +931,14 @@ msgid ""
900931"pointer can be safely cast to both ``PyListObject *`` and ``SubListObject "
901932"*``::"
902933msgstr ""
934+ "当一个 Python 对象是 :class:`!SubList` 的实例时,它的 ``PyObject *`` 指针可以被安全地强制转换为 "
935+ "``PyListObject *`` 和 ``SubListObject *``::"
903936
904937#: ../../extending/newtypes_tutorial.rst:838
905938msgid ""
906939"We see above how to call through to the :meth:`~object.__init__` method of "
907940"the base type."
908- msgstr ""
941+ msgstr "我们可以在上面看到如何将调用传递到基类型的 :meth:`~object.__init__` 方法。 "
909942
910943#: ../../extending/newtypes_tutorial.rst:841
911944msgid ""
@@ -950,6 +983,7 @@ msgid ""
950983"After that, calling :c:func:`PyType_Ready` and adding the type object to the"
951984" module is the same as with the basic :class:`!Custom` examples."
952985msgstr ""
986+ "在那之后,调用 :c:func:`PyType_Ready` 并将类型对象添加到模块中的过程与基本的 :class:`!Custom` 示例是一样的。"
953987
954988#: ../../extending/newtypes_tutorial.rst:886
955989msgid "Footnotes"
0 commit comments