55#
66# Translators:
77# Freesand Leo <[email protected] >, 20228+ # Alpha Du <[email protected] >, 202289#
910#, fuzzy
1011msgid ""
@@ -13,7 +14,7 @@ msgstr ""
1314"Report-Msgid-Bugs-To : \n "
1415"POT-Creation-Date : 2022-06-16 06:28+0000\n "
1516"PO-Revision-Date : 2021-06-28 00:54+0000\n "
16- "Last-Translator : Freesand Leo <yuqinju@163 .com>, 2022\n "
17+ "Last-Translator : Alpha Du <alphanow@gmail .com>, 2022\n "
1718"Language-Team : Chinese (China) (https://www.transifex.com/python-doc/teams/5390/zh_CN/)\n "
1819"MIME-Version : 1.0\n "
1920"Content-Type : text/plain; charset=UTF-8\n "
@@ -107,22 +108,22 @@ msgstr "作为文件参数传给 Python 解释器的 Python 模块:"
107108msgid ""
108109"the Python module or package passed to the Python interpreter with the "
109110":option:`-m` argument:"
110- msgstr ""
111+ msgstr "作为 :option:`-m` 参数传给 Python 解释器的 Python 模块或包: "
111112
112113#: ../../library/__main__.rst:75
113114msgid "Python code read by the Python interpreter from standard input:"
114- msgstr ""
115+ msgstr "Python 解释器从标准输入中读取的 Python 代码: "
115116
116117#: ../../library/__main__.rst:86
117118msgid ""
118119"Python code passed to the Python interpreter with the :option:`-c` argument:"
119- msgstr ""
120+ msgstr "作为 :option:`-c` 参数传递给 Python 解释器的 Python 代码: "
120121
121122#: ../../library/__main__.rst:97
122123msgid ""
123124"In each of these situations, the top-level module's ``__name__`` is set to "
124125"``'__main__'``."
125- msgstr ""
126+ msgstr "在以上每个情形中,顶级模块的 ``__name__`` 被设置为 ``'__main__'`` 。 "
126127
127128#: ../../library/__main__.rst:100
128129msgid ""
@@ -131,16 +132,18 @@ msgid ""
131132"idiom for conditionally executing code when the module is not initialized "
132133"from an import statement::"
133134msgstr ""
135+ "因此,一个模块可以通过检查自己的 ``__name__`` "
136+ ",来发现它是否在顶层环境中运行。这是允许在模块没有从导入语句中初始化的情况下,有条件地执行代码的一个常见的语句::"
134137
135138#: ../../library/__main__.rst:111
136139msgid ""
137140"For a more detailed look at how ``__name__`` is set in all situations, see "
138141"the tutorial section :ref:`tut-modules`."
139- msgstr ""
142+ msgstr "关于在所有情况下 ``__name__`` 是如何设置的细节,请看教程部分 :ref:`tut-modules` 。 "
140143
141144#: ../../library/__main__.rst:116 ../../library/__main__.rst:239
142145msgid "Idiomatic Usage"
143- msgstr ""
146+ msgstr "常见用法 "
144147
145148#: ../../library/__main__.rst:118
146149msgid ""
@@ -149,20 +152,25 @@ msgid ""
149152"like this was imported from a different module, for example to unit test it,"
150153" the script code would unintentionally execute as well."
151154msgstr ""
155+ "有些模块包含了仅供脚本使用的代码,比如解析命令行参数或从标准输入获取数据。 "
156+ "如果这样的模块被从不同的模块中导入,例如为了单元测试,脚本代码也会无意中执行。"
152157
153158#: ../../library/__main__.rst:123
154159msgid ""
155160"This is where using the ``if __name__ == '__main__'`` code block comes in "
156161"handy. Code within this block won't run unless the module is executed in the"
157162" top-level environment."
158163msgstr ""
164+ "这就是 ``if __name__ == '__main__'`` 代码块的用武之地。除非模块在顶层环境中被执行,否则该块内的代码不会运行。"
159165
160166#: ../../library/__main__.rst:127
161167msgid ""
162168"Putting as few statements as possible in the block below ``if __name___ == "
163169"'__main__'`` can improve code clarity and correctness. Most often, a "
164170"function named ``main`` encapsulates the program's primary behavior::"
165171msgstr ""
172+ "将尽可能少的语句放在下面的 ``if __name___ == '__main__'`` 块中可以提高代码的清晰度和正确性。最常见的,一个名为 "
173+ "``main`` 的函数封装了程序的主要行为::"
166174
167175#: ../../library/__main__.rst:151
168176msgid ""
@@ -173,6 +181,9 @@ msgid ""
173181"using the global variable instead of a local name. A ``main`` function "
174182"solves this problem."
175183msgstr ""
184+ "请注意,如果模块没有将代码封装在 ``main`` 函数内,而是直接放在 ``if __name__ == '__main__'`` 块内,那么这个 "
185+ "``phrase`` 变量对整个模块来说就是全局变量。 这很容易出错,因为模块内的其他函数可能会无意中使用全局变量而不是局部名称。 一个 "
186+ "``main`` 函数解决了这个问题。"
176187
177188#: ../../library/__main__.rst:158
178189msgid ""
@@ -181,10 +192,12 @@ msgid ""
181192"imported, the ``echo`` and ``main`` functions will be defined, but neither "
182193"of them will be called, because ``__name__ != '__main__'``."
183194msgstr ""
195+ "使用 ``main`` 函数有一个额外的好处,就是 ``echo`` 函数本身是孤立的,可以在其他地方导入。当 ``echo.py`` "
196+ "被导入时,``echo`` 和 ``main`` 函数将被定义,但它们都不会被调用,因为 ``__name__ != '__main__'`` 。"
184197
185198#: ../../library/__main__.rst:165
186199msgid "Packaging Considerations"
187- msgstr ""
200+ msgstr "打包考量 "
188201
189202#: ../../library/__main__.rst:167
190203msgid ""
@@ -194,6 +207,8 @@ msgid ""
194207"where the return value of ``main`` is passed into :func:`sys.exit`. For "
195208"example::"
196209msgstr ""
210+ "``main`` 函数经常被用来创建命令行工具,把它们指定为控制台脚本的入口点。 当这样做时,`pip <https://pip.pypa.io/>`_"
211+ " 将函数调用插入到模板脚本中,其中 ``main`` 的返回值被传递到 :func:`sys.exit` 。例如::"
197212
198213#: ../../library/__main__.rst:175
199214msgid ""
@@ -202,6 +217,8 @@ msgid ""
202217":func:`sys.exit`; typically, an integer or ``None`` (which is implicitly "
203218"returned if your function does not have a return statement)."
204219msgstr ""
220+ "由于 ``main`` 调用被包裹在 :func:`sys.exit` 中,期望你的函数将返回一些可被 :func:`sys.exit` "
221+ "作为输入而接受的值;通常为一个整数或 ``None`` (如果你的函数没有返回语句,则隐含返回)。"
205222
206223#: ../../library/__main__.rst:180
207224msgid ""
@@ -210,6 +227,8 @@ msgid ""
210227"if we later package it as a console script entry-point in a pip-installable "
211228"package."
212229msgstr ""
230+ "通过主动遵循这一惯例,我们的模块在直接运行时(即 ``python3 echo.py`` )会有相同的行为,如果我们以后把它打包成可用 pip "
231+ "安装的包中的控制台脚本入口,它也会有相同的行为。"
213232
214233#: ../../library/__main__.rst:185
215234msgid ""
@@ -220,17 +239,22 @@ msgid ""
220239"``echo.py`` example from earlier exemplifies using the ``sys.exit(main())`` "
221240"convention."
222241msgstr ""
242+ "特别的是,要小心从你的 ``main`` 函数中返回字符串。 :func:`sys.exit` 将把一个字符串参数解释为失败信息,所以你的程序将有一个 "
243+ "``1`` 的退出代码,表示失败。并且这个字符串将被写入 :data:`sys.stderr` 。 前面的 ``echo.py`` 例子举例说明了使用 "
244+ "``sys.exit(main())`` 的约定。"
223245
224246#: ../../library/__main__.rst:193
225247msgid ""
226248"`Python Packaging User Guide <https://packaging.python.org/>`_ contains a "
227249"collection of tutorials and references on how to distribute and install "
228250"Python packages with modern tools."
229251msgstr ""
252+ "`Python 打包用户指南 <https://packaging.python.org/>`_ 包含了一系列关于如何用现代工具分发和安装 Python"
253+ " 包的教程和参考资料。"
230254
231255#: ../../library/__main__.rst:199
232256msgid "``__main__.py`` in Python Packages"
233- msgstr ""
257+ msgstr "Python 包中的 ``__main__.py`` "
234258
235259#: ../../library/__main__.rst:201
236260msgid ""
@@ -239,12 +263,14 @@ msgid ""
239263"to provide a command-line interface for a package. Consider the following "
240264"hypothetical package, \" bandclass\" :"
241265msgstr ""
266+ "如果你不熟悉Python包,请参阅本教程的 :ref:`tut-packages` 一节。最常见的是, ``__main__.py`` "
267+ "文件被用来为一个包提供命令行接口。假设有下面这个虚构的包,\" bandclass\" :"
242268
243269#: ../../library/__main__.rst:213
244270msgid ""
245271"``__main__.py`` will be executed when the package itself is invoked directly"
246272" from the command line using the :option:`-m` flag. For example:"
247- msgstr ""
273+ msgstr "当使用 :option:`-m` 标志从命令行直接调用软件包本身时,将执行 ``__main__.py`` 。比如说。 "
248274
249275#: ../../library/__main__.rst:220
250276msgid ""
@@ -253,6 +279,8 @@ msgid ""
253279"this hypothetical case, it might make sense to allow the teacher to search "
254280"for students::"
255281msgstr ""
282+ "这个命令将导致 ``__main__.py`` "
283+ "的运行。你如何利用这一机制将取决于你所编写的软件包的性质,但在这个假设的案例中,允许教师搜索学生可能是有意义的::"
256284
257285#: ../../library/__main__.rst:233
258286msgid ""
@@ -261,6 +289,9 @@ msgid ""
261289"within a package. For more details, see :ref:`intra-package-references` in "
262290"the :ref:`tut-modules` section of the tutorial."
263291msgstr ""
292+ "注意, ``from .student import search_students`` 是一个相对导入的例子。 "
293+ "这种导入方式可以在引用一个包内的模块时使用。 更多细节,请参见教程 :ref:`tut-modules` 中的 :ref:`intra-package-"
294+ "references` 一节。"
264295
265296#: ../../library/__main__.rst:241
266297msgid ""
@@ -269,44 +300,54 @@ msgid ""
269300"execute from other modules. Those other modules can then be easily unit-"
270301"tested and are properly reusable."
271302msgstr ""
303+ "``__main__.py`` 的内容通常不是用 ``if __name__ == '__main__'`` 区块围起来的。 "
304+ "相反,这些文件保持简短,功能从其他模块执行。 那些其他模块可以很容易地进行单元测试,并且可以适当地重复使用。"
272305
273306#: ../../library/__main__.rst:246
274307msgid ""
275308"If used, an ``if __name__ == '__main__'`` block will still work as expected "
276309"for a ``__main__.py`` file within a package, because its ``__name__`` "
277310"attribute will include the package's path if imported::"
278311msgstr ""
312+ "如果使用,一个 ``if __name__ == '__main__'`` 区块仍然会像预期的那样对包内的 ``__main__.py`` "
313+ "文件起作用,因为如果导入,它的 ``__name__`` 属性将包括包的路径::"
279314
280315#: ../../library/__main__.rst:254
281316msgid ""
282317"This won't work for ``__main__.py`` files in the root directory of a .zip "
283318"file though. Hence, for consistency, minimal ``__main__.py`` like the "
284319":mod:`venv` one mentioned below are preferred."
285320msgstr ""
321+ "但这对 .zip 文件的根目录中的 ``__main__.py`` 文件不起作用。 因此,为了保持一致性,像下面提到的 :mod:`venv` "
322+ "这样的最小 ``__main__.py`` 是首选。"
286323
287324#: ../../library/__main__.rst:260
288325msgid ""
289326"See :mod:`venv` for an example of a package with a minimal ``__main__.py`` "
290327"in the standard library. It doesn't contain a ``if __name__ == '__main__'`` "
291328"block. You can invoke it with ``python3 -m venv [directory]``."
292329msgstr ""
330+ "参见 :mod:`venv` 以了解标准库中最小 ``__main__.py`` 的软件包示例。它不包含一个 ``if __name__ == "
331+ "'__main__'`` 块。你可以用 ``python3 -m venv [directory]`` 调用它。"
293332
294333#: ../../library/__main__.rst:264
295334msgid ""
296335"See :mod:`runpy` for more details on the :option:`-m` flag to the "
297336"interpreter executable."
298- msgstr ""
337+ msgstr "参见 :mod:`runpy` 以了解更多关于 :option:`-m` 标志对解释器可执行包的细节。 "
299338
300339#: ../../library/__main__.rst:267
301340msgid ""
302341"See :mod:`zipapp` for how to run applications packaged as *.zip* files. In "
303342"this case Python looks for a ``__main__.py`` file in the root directory of "
304343"the archive."
305344msgstr ""
345+ "参见 :mod:`zipapp` 了解如何运行打包成 *.zip* 文件的应用程序。在这种情况下,Python 会在归档文件的根目录下寻找一个 "
346+ "``__main__.py`` 文件。"
306347
307348#: ../../library/__main__.rst:274
308349msgid "``import __main__``"
309- msgstr ""
350+ msgstr "``import __main__`` "
310351
311352#: ../../library/__main__.rst:276
312353msgid ""
@@ -316,25 +357,30 @@ msgid ""
316357"doesn't import a ``__main__.py`` file but rather whichever module that "
317358"received the special name ``'__main__'``."
318359msgstr ""
360+ "不管 Python 程序是用哪个模块启动的,在同一程序中运行的其他模块可以通过导入 ``__main__`` 模块来导入顶级环境的范围 ( "
361+ ":term:`namespace` )。这并不是导入一个 ``__main__.py`` 文件,而是导入使用特殊名称 ``'__main__'`` "
362+ "的哪个模块。"
319363
320364#: ../../library/__main__.rst:282
321365msgid "Here is an example module that consumes the ``__main__`` namespace::"
322- msgstr ""
366+ msgstr "下面是一个使用 ``__main__`` 命名空间的模块的例子:: "
323367
324368#: ../../library/__main__.rst:300
325369msgid "Example usage of this module could be as follows::"
326- msgstr ""
370+ msgstr "该模块的用法示例如下:: "
327371
328372#: ../../library/__main__.rst:319
329373msgid "Now, if we started our program, the result would look like this:"
330- msgstr ""
374+ msgstr "现在,如果我们启动我们的程序,结果会是这样的: "
331375
332376#: ../../library/__main__.rst:326
333377msgid ""
334378"The exit code of the program would be 1, indicating an error. Uncommenting "
335379"the line with ``my_name = \" Dinsdale\" `` fixes the program and now it exits "
336380"with status code 0, indicating success:"
337381msgstr ""
382+ "该程序的退出代码为 1 ,表明有错误。取消对 ``my_name = \" Dinsdale\" `` 这一行的注释,就可以修复程序,现在它的退出状态代码为"
383+ " 0 ,表示成功。"
338384
339385#: ../../library/__main__.rst:335
340386msgid ""
@@ -343,6 +389,8 @@ msgid ""
343389"the ``if __name__ == \" __main__\" `` block of the ``start`` module. Why does "
344390"this work?"
345391msgstr ""
392+ "请注意,导入 ``__main__`` 不会导致无意中运行旨在用于脚本的顶层代码的问题,这些代码被放在模块 ``start`` 的 ``if "
393+ "__name__ == \" __main__\" `` 块中。为什么这样做?"
346394
347395#: ../../library/__main__.rst:339
348396msgid ""
0 commit comments