-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathmodules.po
687 lines (606 loc) · 36.6 KB
/
modules.po
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
# SOME DESCRIPTIVE TITLE.
# Copyright (C) 2001-2024, Python Software Foundation
# This file is distributed under the same license as the Python package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
# Translators:
# eric R <trencyclopedia@gmail.com>, 2018
# ww song <sww4718168@gmail.com>, 2018
# df2dc1c92e792f7ae8417c51df43db8f_594d92a <0f49be28017426edb1db1a2ab6e67088_717605>, 2018
# jaystone776 <1732865113@qq.com>, 2021
# Alpha Du <alphanow@gmail.com>, 2022
# Rafael Fontenelle <rffontenelle@gmail.com>, 2023
# Freesand Leo <yuqinju@163.com>, 2024
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: Python 3.9\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-10-11 16:34+0000\n"
"PO-Revision-Date: 2017-02-16 23:41+0000\n"
"Last-Translator: Freesand Leo <yuqinju@163.com>, 2024\n"
"Language-Team: Chinese (China) (https://app.transifex.com/python-doc/teams/5390/zh_CN/)\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Language: zh_CN\n"
"Plural-Forms: nplurals=1; plural=0;\n"
#: ../../tutorial/modules.rst:5
msgid "Modules"
msgstr "模块"
#: ../../tutorial/modules.rst:7
msgid ""
"If you quit from the Python interpreter and enter it again, the definitions "
"you have made (functions and variables) are lost. Therefore, if you want to "
"write a somewhat longer program, you are better off using a text editor to "
"prepare the input for the interpreter and running it with that file as input"
" instead. This is known as creating a *script*. As your program gets "
"longer, you may want to split it into several files for easier maintenance."
" You may also want to use a handy function that you've written in several "
"programs without copying its definition into each program."
msgstr ""
"退出 Python 解释器后,再次进入时,之前在 Python "
"解释器中定义的函数和变量就丢失了。因此,编写较长程序时,最好用文本编辑器代替解释器,执行文件中的输入内容,这就是编写 *脚本* "
"。随着程序越来越长,为了方便维护,最好把脚本拆分成多个文件。编写脚本还一个好处,不同程序调用同一个函数时,不用把函数定义复制到各个程序。"
#: ../../tutorial/modules.rst:16
msgid ""
"To support this, Python has a way to put definitions in a file and use them "
"in a script or in an interactive instance of the interpreter. Such a file is"
" called a *module*; definitions from a module can be *imported* into other "
"modules or into the *main* module (the collection of variables that you have"
" access to in a script executed at the top level and in calculator mode)."
msgstr ""
"为实现这些需求,Python 把各种定义存入一个文件,在脚本或解释器的交互式实例中使用。这个文件就是 *模块* ;模块中的定义可以 *导入* "
"到其他模块或 *主* 模块(在顶层和计算器模式下,执行脚本中可访问的变量集)。"
#: ../../tutorial/modules.rst:22
msgid ""
"A module is a file containing Python definitions and statements. The file "
"name is the module name with the suffix :file:`.py` appended. Within a "
"module, the module's name (as a string) is available as the value of the "
"global variable ``__name__``. For instance, use your favorite text editor "
"to create a file called :file:`fibo.py` in the current directory with the "
"following contents::"
msgstr ""
"模块是包含 Python 定义和语句的文件。其文件名是模块名加后缀名 :file:`.py` 。在模块内部,通过全局变量 ``__name__`` "
"可以获取模块名(即字符串)。例如,用文本编辑器在当前目录下创建 :file:`fibo.py` 文件,输入以下内容:"
#: ../../tutorial/modules.rst:45
msgid ""
"Now enter the Python interpreter and import this module with the following "
"command::"
msgstr "现在,进入 Python 解释器,用以下命令导入该模块:"
#: ../../tutorial/modules.rst:50
msgid ""
"This does not enter the names of the functions defined in ``fibo`` directly"
" in the current symbol table; it only enters the module name ``fibo`` there."
" Using the module name you can access the functions::"
msgstr "这项操作不直接把 ``fibo`` 函数定义的名称导入到当前符号表,只导入模块名 ``fibo`` 。要使用模块名访问函数:"
#: ../../tutorial/modules.rst:61
msgid ""
"If you intend to use a function often you can assign it to a local name::"
msgstr "如果经常使用某个函数,可以把它赋值给局部变量:"
#: ../../tutorial/modules.rst:71
msgid "More on Modules"
msgstr "模块详解"
#: ../../tutorial/modules.rst:73
msgid ""
"A module can contain executable statements as well as function definitions. "
"These statements are intended to initialize the module. They are executed "
"only the *first* time the module name is encountered in an import statement."
" [#]_ (They are also run if the file is executed as a script.)"
msgstr ""
"模块包含可执行语句及函数定义。这些语句用于初始化模块,且仅在 import 语句 *第一次* 遇到模块名时执行。[#]_ "
"(文件作为脚本运行时,也会执行这些语句。)"
#: ../../tutorial/modules.rst:78
msgid ""
"Each module has its own private symbol table, which is used as the global "
"symbol table by all functions defined in the module. Thus, the author of a "
"module can use global variables in the module without worrying about "
"accidental clashes with a user's global variables. On the other hand, if you"
" know what you are doing you can touch a module's global variables with the "
"same notation used to refer to its functions, ``modname.itemname``."
msgstr ""
"模块有自己的私有符号表,用作模块中所有函数的全局符号表。因此,在模块内使用全局变量时,不用担心与用户定义的全局变量发生冲突。另一方面,可以用与访问模块函数一样的标记法,访问模块的全局变量,``modname.itemname``。"
#: ../../tutorial/modules.rst:85
msgid ""
"Modules can import other modules. It is customary but not required to place"
" all :keyword:`import` statements at the beginning of a module (or script, "
"for that matter). The imported module names are placed in the importing "
"module's global symbol table."
msgstr ""
"可以把其他模块导入模块。 按惯例,所有 :keyword:`import` 语句都放在模块(或脚本)开头,但这不是必须的。 "
"被导入的模块名存在导入方模块的全局符号表里。"
#: ../../tutorial/modules.rst:90
msgid ""
"There is a variant of the :keyword:`import` statement that imports names "
"from a module directly into the importing module's symbol table. For "
"example::"
msgstr ":keyword:`import` 语句有一个变体,可以直接把模块里的名称导入到另一个模块的符号表。例如:"
#: ../../tutorial/modules.rst:97
msgid ""
"This does not introduce the module name from which the imports are taken in "
"the local symbol table (so in the example, ``fibo`` is not defined)."
msgstr "这段代码不会把模块名导入到局部符号表里(因此,本例没有定义 ``fibo``)。"
#: ../../tutorial/modules.rst:100
msgid "There is even a variant to import all names that a module defines::"
msgstr "还有一种变体可以导入模块内定义的所有名称:"
#: ../../tutorial/modules.rst:106
msgid ""
"This imports all names except those beginning with an underscore (``_``). In"
" most cases Python programmers do not use this facility since it introduces "
"an unknown set of names into the interpreter, possibly hiding some things "
"you have already defined."
msgstr ""
"这种方式会导入所有不以下划线(``_``)开头的名称。大多数情况下,不要用这个功能,这种方式向解释器导入了一批未知的名称,可能会覆盖已经定义的名称。"
#: ../../tutorial/modules.rst:111
msgid ""
"Note that in general the practice of importing ``*`` from a module or "
"package is frowned upon, since it often causes poorly readable code. "
"However, it is okay to use it to save typing in interactive sessions."
msgstr ""
"注意,一般情况下,不建议从模块或包内导入 ``*``,因为,这项操作经常让代码变得难以理解。不过,为了在交互式会话中少打几个字,这么用也没问题。"
#: ../../tutorial/modules.rst:115
msgid ""
"If the module name is followed by :keyword:`!as`, then the name following "
":keyword:`!as` is bound directly to the imported module."
msgstr "模块名后使用 :keyword:`!as` 时,直接把 :keyword:`!as` 后的名称与导入模块绑定。"
#: ../../tutorial/modules.rst:124
msgid ""
"This is effectively importing the module in the same way that ``import "
"fibo`` will do, with the only difference of it being available as ``fib``."
msgstr "与 ``import fibo`` 一样,这种方式也可以有效地导入模块,唯一的区别是,导入的名称是 ``fib``。"
#: ../../tutorial/modules.rst:127
msgid ""
"It can also be used when utilising :keyword:`from` with similar effects::"
msgstr ":keyword:`from` 中也可以使用这种方式,效果类似:"
#: ../../tutorial/modules.rst:136
msgid ""
"For efficiency reasons, each module is only imported once per interpreter "
"session. Therefore, if you change your modules, you must restart the "
"interpreter -- or, if it's just one module you want to test interactively, "
"use :func:`importlib.reload`, e.g. ``import importlib; "
"importlib.reload(modulename)``."
msgstr ""
"为了保证运行效率,每次解释器会话只导入一次模块。如果更改了模块内容,必须重启解释器;仅交互测试一个模块时,也可以使用 "
":func:`importlib.reload`,例如 ``import importlib; "
"importlib.reload(modulename)``。"
#: ../../tutorial/modules.rst:146
msgid "Executing modules as scripts"
msgstr "以脚本方式执行模块"
#: ../../tutorial/modules.rst:148
msgid "When you run a Python module with ::"
msgstr "可以用以下方式运行 Python 模块:"
#: ../../tutorial/modules.rst:152
msgid ""
"the code in the module will be executed, just as if you imported it, but "
"with the ``__name__`` set to ``\"__main__\"``. That means that by adding "
"this code at the end of your module::"
msgstr ""
"这项操作将执行模块里的代码,和导入模块一样,但会把 ``__name__`` 赋值为 ``\"__main__\"``。 "
"也就是把下列代码添加到模块末尾:"
#: ../../tutorial/modules.rst:160
msgid ""
"you can make the file usable as a script as well as an importable module, "
"because the code that parses the command line only runs if the module is "
"executed as the \"main\" file:"
msgstr "这个文件既能被用作脚本,又能被用作一个可供导入的模块,因为解析命令行参数的那两行代码只有在模块作为“main”文件执行时才会运行:"
#: ../../tutorial/modules.rst:169
msgid "If the module is imported, the code is not run::"
msgstr "当这个模块被导入到其它模块时,那两行代码不运行:"
#: ../../tutorial/modules.rst:174
msgid ""
"This is often used either to provide a convenient user interface to a "
"module, or for testing purposes (running the module as a script executes a "
"test suite)."
msgstr "这常用于为模块提供一个便捷的用户接口,或用于测试(把模块作为执行测试套件的脚本运行)。"
#: ../../tutorial/modules.rst:181
msgid "The Module Search Path"
msgstr "模块搜索路径"
#: ../../tutorial/modules.rst:185
msgid ""
"When a module named :mod:`spam` is imported, the interpreter first searches "
"for a built-in module with that name. These module names are listed in "
":data:`sys.builtin_module_names`. If not found, it then searches for a file "
"named :file:`spam.py` in a list of directories given by the variable "
":data:`sys.path`. :data:`sys.path` is initialized from these locations:"
msgstr ""
"当一个名为 :mod:`spam` 的模块被导入时,解释器首先搜索具有该名称的内置模块。这些模块的名字被列在 "
":data:`sys.builtin_module_names` 中。如果没有找到,它就在变量 :data:`sys.path` "
"给出的目录列表中搜索一个名为 :file:`spam.py` 的文件,:data:`sys.path` 从这些位置初始化:"
#: ../../tutorial/modules.rst:191
msgid ""
"The directory containing the input script (or the current directory when no "
"file is specified)."
msgstr "被命令行直接运行的脚本所在的目录(或未指定文件时的当前目录)。"
#: ../../tutorial/modules.rst:193
msgid ""
":envvar:`PYTHONPATH` (a list of directory names, with the same syntax as the"
" shell variable :envvar:`PATH`)."
msgstr ":envvar:`PYTHONPATH` (目录列表,与 shell 变量 :envvar:`PATH` 的语法一样)。"
#: ../../tutorial/modules.rst:195
msgid ""
"The installation-dependent default (by convention including a ``site-"
"packages`` directory, handled by the :mod:`site` module)."
msgstr "依赖于安装的默认值(按照惯例包括一个 ``site-packages`` 目录,由 :mod:`site` 模块处理)。"
#: ../../tutorial/modules.rst:199
msgid ""
"On file systems which support symlinks, the directory containing the input "
"script is calculated after the symlink is followed. In other words the "
"directory containing the symlink is **not** added to the module search path."
msgstr ""
"在支持符号链接的文件系统中,“被命令行直接运行的脚本所在的目录”是符号链接最终指向的目录。换句话说,符号链接所在的目录并 **没有** "
"被添加至模块搜索路径。"
#: ../../tutorial/modules.rst:203
msgid ""
"After initialization, Python programs can modify :data:`sys.path`. The "
"directory containing the script being run is placed at the beginning of the "
"search path, ahead of the standard library path. This means that scripts in "
"that directory will be loaded instead of modules of the same name in the "
"library directory. This is an error unless the replacement is intended. See"
" section :ref:`tut-standardmodules` for more information."
msgstr ""
"初始化后,Python 程序可以更改 "
":data:`sys.path`。脚本所在的目录先于标准库所在的路径被搜索。这意味着,脚本所在的目录如果有和标准库同名的文件,那么加载的是该目录里的,而不是标准库的。这一般是一个错误,除非这样的替换是你有意为之。详见"
" :ref:`tut-standardmodules`。"
#: ../../tutorial/modules.rst:214
msgid "\"Compiled\" Python files"
msgstr "“已编译的” Python 文件"
#: ../../tutorial/modules.rst:216
msgid ""
"To speed up loading modules, Python caches the compiled version of each "
"module in the ``__pycache__`` directory under the name "
":file:`module.{version}.pyc`, where the version encodes the format of the "
"compiled file; it generally contains the Python version number. For "
"example, in CPython release 3.3 the compiled version of spam.py would be "
"cached as ``__pycache__/spam.cpython-33.pyc``. This naming convention "
"allows compiled modules from different releases and different versions of "
"Python to coexist."
msgstr ""
"为了快速加载模块,Python 把模块的编译版本缓存在 ``__pycache__`` 目录中,文件名为 "
":file:`module.{version}.pyc`,version 对编译文件格式进行编码,一般是 Python 的版本号。例如,CPython "
"的 3.3 发行版中,spam.py 的编译版本缓存为 ``__pycache__/spam.cpython-33.pyc``。这种命名惯例让不同 "
"Python 版本编译的模块可以共存。"
#: ../../tutorial/modules.rst:224
msgid ""
"Python checks the modification date of the source against the compiled "
"version to see if it's out of date and needs to be recompiled. This is a "
"completely automatic process. Also, the compiled modules are platform-"
"independent, so the same library can be shared among systems with different "
"architectures."
msgstr ""
"Python "
"对比编译版与源码的修改日期,查看编译版是否已过期,是否要重新编译。此进程完全是自动的。此外,编译模块与平台无关,因此,可在不同架构的系统之间共享相同的库。"
#: ../../tutorial/modules.rst:229
msgid ""
"Python does not check the cache in two circumstances. First, it always "
"recompiles and does not store the result for the module that's loaded "
"directly from the command line. Second, it does not check the cache if "
"there is no source module. To support a non-source (compiled only) "
"distribution, the compiled module must be in the source directory, and there"
" must not be a source module."
msgstr ""
"Python "
"在两种情况下不检查缓存。一,从命令行直接载入的模块,每次都会重新编译,且不储存编译结果;二,没有源模块,就不会检查缓存。为了让一个库能以隐藏源代码的形式分发(通过将所有源代码变为编译后的版本),编译后的模块必须放在源目录而非缓存目录中,并且源目录绝不能包含同名的未编译的源模块。"
#: ../../tutorial/modules.rst:236
msgid "Some tips for experts:"
msgstr "给专业人士的一些小建议:"
#: ../../tutorial/modules.rst:238
msgid ""
"You can use the :option:`-O` or :option:`-OO` switches on the Python command"
" to reduce the size of a compiled module. The ``-O`` switch removes assert "
"statements, the ``-OO`` switch removes both assert statements and __doc__ "
"strings. Since some programs may rely on having these available, you should"
" only use this option if you know what you're doing. \"Optimized\" modules "
"have an ``opt-`` tag and are usually smaller. Future releases may change "
"the effects of optimization."
msgstr ""
"在 Python 命令中使用 :option:`-O` 或 :option:`-OO` 开关,可以减小编译模块的大小。``-O`` "
"去除断言语句,``-OO`` 去除断言语句和 __doc__ "
"字符串。有些程序可能依赖于这些内容,因此,没有十足的把握,不要使用这两个选项。“优化过的”模块带有 ``opt-`` "
"标签,并且文件通常会一小些。将来的发行版或许会改进优化的效果。"
#: ../../tutorial/modules.rst:246
msgid ""
"A program doesn't run any faster when it is read from a ``.pyc`` file than "
"when it is read from a ``.py`` file; the only thing that's faster about "
"``.pyc`` files is the speed with which they are loaded."
msgstr "从 ``.pyc`` 文件读取的程序不比从 ``.py`` 读取的执行速度快,``.pyc`` 文件只是加载速度更快。"
#: ../../tutorial/modules.rst:250
msgid ""
"The module :mod:`compileall` can create .pyc files for all modules in a "
"directory."
msgstr ":mod:`compileall` 模块可以为一个目录下的所有模块创建 .pyc 文件。"
#: ../../tutorial/modules.rst:253
msgid ""
"There is more detail on this process, including a flow chart of the "
"decisions, in :pep:`3147`."
msgstr "本过程的细节及决策流程图,详见 :pep:`3147`。"
#: ../../tutorial/modules.rst:260
msgid "Standard Modules"
msgstr "标准模块"
#: ../../tutorial/modules.rst:264
msgid ""
"Python comes with a library of standard modules, described in a separate "
"document, the Python Library Reference (\"Library Reference\" hereafter). "
"Some modules are built into the interpreter; these provide access to "
"operations that are not part of the core of the language but are "
"nevertheless built in, either for efficiency or to provide access to "
"operating system primitives such as system calls. The set of such modules "
"is a configuration option which also depends on the underlying platform. "
"For example, the :mod:`winreg` module is only provided on Windows systems. "
"One particular module deserves some attention: :mod:`sys`, which is built "
"into every Python interpreter. The variables ``sys.ps1`` and ``sys.ps2`` "
"define the strings used as primary and secondary prompts::"
msgstr ""
"Python 自带一个标准模块的库,它在 Python 库参考(此处以下称为\"库参考\" )里另外描述。 一些模块是内嵌到解释器里面的, "
"它们给一些虽并非语言核心但却内嵌的操作提供接口,要么是为了效率,要么是给操作系统基础操作例如系统调入提供接口。 这些模块集是一个配置选项, "
"并且还依赖于底层的操作系统。 例如,:mod:`winreg` 模块只在 Windows 系统上提供。一个特别值得注意的模块 "
":mod:`sys`,它被内嵌到每一个 Python 解释器中。``sys.ps1`` 和 ``sys.ps2`` "
"变量定义了一些字符,它们可以用作主提示符和辅助提示符::"
#: ../../tutorial/modules.rst:287
msgid ""
"These two variables are only defined if the interpreter is in interactive "
"mode."
msgstr "只有解释器用于交互模式时,才定义这两个变量。"
#: ../../tutorial/modules.rst:289
msgid ""
"The variable ``sys.path`` is a list of strings that determines the "
"interpreter's search path for modules. It is initialized to a default path "
"taken from the environment variable :envvar:`PYTHONPATH`, or from a built-in"
" default if :envvar:`PYTHONPATH` is not set. You can modify it using "
"standard list operations::"
msgstr ""
"变量 ``sys.path`` 是字符串列表,用于确定解释器的模块搜索路径。该变量以环境变量 :envvar:`PYTHONPATH` "
"提取的默认路径进行初始化,如未设置 :envvar:`PYTHONPATH`,则使用内置的默认路径。可以用标准列表操作修改该变量:"
#: ../../tutorial/modules.rst:302
msgid "The :func:`dir` Function"
msgstr ":func:`dir` 函数"
#: ../../tutorial/modules.rst:304
msgid ""
"The built-in function :func:`dir` is used to find out which names a module "
"defines. It returns a sorted list of strings::"
msgstr "内置函数 :func:`dir` 用于查找模块定义的名称。返回结果是经过排序的字符串列表:"
#: ../../tutorial/modules.rst:333
msgid ""
"Without arguments, :func:`dir` lists the names you have defined currently::"
msgstr "没有参数时,:func:`dir` 列出当前已定义的名称:"
#: ../../tutorial/modules.rst:341
msgid ""
"Note that it lists all types of names: variables, modules, functions, etc."
msgstr "注意它列出所有类型的名称:变量,模块,函数,……。"
#: ../../tutorial/modules.rst:345
msgid ""
":func:`dir` does not list the names of built-in functions and variables. If"
" you want a list of those, they are defined in the standard module "
":mod:`builtins`::"
msgstr ":func:`dir` 不会列出内置函数和变量的名称。这些内容的定义在标准模块 :mod:`builtins` 中:"
#: ../../tutorial/modules.rst:384
msgid "Packages"
msgstr "包"
#: ../../tutorial/modules.rst:386
msgid ""
"Packages are a way of structuring Python's module namespace by using "
"\"dotted module names\". For example, the module name :mod:`A.B` designates"
" a submodule named ``B`` in a package named ``A``. Just like the use of "
"modules saves the authors of different modules from having to worry about "
"each other's global variable names, the use of dotted module names saves the"
" authors of multi-module packages like NumPy or Pillow from having to worry "
"about each other's module names."
msgstr ""
"包是一种用“点式模块名”构造 Python 模块命名空间的方法。例如,模块名 :mod:`A.B` 表示包 ``A`` 中名为 ``B`` "
"的子模块。正如模块可以区分不同模块之间的全局变量名称一样,点式模块名可以区分 NumPy 或 Pillow 等不同多模块包之间的模块名称。"
#: ../../tutorial/modules.rst:394
msgid ""
"Suppose you want to design a collection of modules (a \"package\") for the "
"uniform handling of sound files and sound data. There are many different "
"sound file formats (usually recognized by their extension, for example: "
":file:`.wav`, :file:`.aiff`, :file:`.au`), so you may need to create and "
"maintain a growing collection of modules for the conversion between the "
"various file formats. There are also many different operations you might "
"want to perform on sound data (such as mixing, adding echo, applying an "
"equalizer function, creating an artificial stereo effect), so in addition "
"you will be writing a never-ending stream of modules to perform these "
"operations. Here's a possible structure for your package (expressed in "
"terms of a hierarchical filesystem):"
msgstr ""
"假设要为统一处理声音文件与声音数据设计一个模块集(“包”)。声音文件的格式很多(通常以扩展名来识别,例如::file:`.wav`,:file:`.aiff`,:file:`.au`),因此,为了不同文件格式之间的转换,需要创建和维护一个不断增长的模块集合。为了实现对声音数据的不同处理(例如,混声、添加回声、均衡器功能、创造人工立体声效果),还要编写无穷无尽的模块流。下面这个分级文件树展示了这个包的架构:"
#: ../../tutorial/modules.rst:431
msgid ""
"When importing the package, Python searches through the directories on "
"``sys.path`` looking for the package subdirectory."
msgstr "导入包时,Python 搜索 ``sys.path`` 里的目录,查找包的子目录。"
#: ../../tutorial/modules.rst:434
msgid ""
"The :file:`__init__.py` files are required to make Python treat directories "
"containing the file as packages. This prevents directories with a common "
"name, such as ``string``, unintentionally hiding valid modules that occur "
"later on the module search path. In the simplest case, :file:`__init__.py` "
"can just be an empty file, but it can also execute initialization code for "
"the package or set the ``__all__`` variable, described later."
msgstr ""
"Python 只把含 :file:`__init__.py` 文件的目录当成包。这样可以防止以 ``string`` "
"等通用名称命名的目录,无意中屏蔽出现在后方模块搜索路径中的有效模块。 最简情况下,:file:`__init__.py` "
"只是一个空文件,但该文件也可以执行包的初始化代码,或设置 ``__all__`` 变量,详见下文。"
#: ../../tutorial/modules.rst:441
msgid ""
"Users of the package can import individual modules from the package, for "
"example::"
msgstr "还可以从包中导入单个模块,例如:"
#: ../../tutorial/modules.rst:446
msgid ""
"This loads the submodule :mod:`sound.effects.echo`. It must be referenced "
"with its full name. ::"
msgstr "这段代码加载子模块 :mod:`sound.effects.echo` ,但引用时必须使用子模块的全名:"
#: ../../tutorial/modules.rst:451
msgid "An alternative way of importing the submodule is::"
msgstr "另一种导入子模块的方法是 :"
#: ../../tutorial/modules.rst:455
msgid ""
"This also loads the submodule :mod:`echo`, and makes it available without "
"its package prefix, so it can be used as follows::"
msgstr "这段代码还可以加载子模块 :mod:`echo` ,不加包前缀也可以使用。因此,可以按如下方式使用:"
#: ../../tutorial/modules.rst:460
msgid ""
"Yet another variation is to import the desired function or variable "
"directly::"
msgstr "Import 语句的另一种变体是直接导入所需的函数或变量:"
#: ../../tutorial/modules.rst:464
msgid ""
"Again, this loads the submodule :mod:`echo`, but this makes its function "
":func:`echofilter` directly available::"
msgstr "同样,这样也会加载子模块 :mod:`echo`,但可以直接使用函数 :func:`echofilter`:"
#: ../../tutorial/modules.rst:469
msgid ""
"Note that when using ``from package import item``, the item can be either a "
"submodule (or subpackage) of the package, or some other name defined in the"
" package, like a function, class or variable. The ``import`` statement "
"first tests whether the item is defined in the package; if not, it assumes "
"it is a module and attempts to load it. If it fails to find it, an "
":exc:`ImportError` exception is raised."
msgstr ""
"注意,使用 ``from package import item`` 时,item "
"可以是包的子模块(或子包),也可以是包中定义的函数、类或变量等其他名称。``import`` 语句首先测试包中是否定义了 "
"item;如果未在包中定义,则假定 item 是模块,并尝试加载。如果找不到 item,则触发 :exc:`ImportError` 异常。"
#: ../../tutorial/modules.rst:476
msgid ""
"Contrarily, when using syntax like ``import item.subitem.subsubitem``, each "
"item except for the last must be a package; the last item can be a module or"
" a package but can't be a class or function or variable defined in the "
"previous item."
msgstr ""
"相反,使用 ``import item.subitem.subsubitem`` 句法时,除最后一项外,每个 item "
"都必须是包;最后一项可以是模块或包,但不能是上一项中定义的类、函数或变量。"
#: ../../tutorial/modules.rst:485
msgid "Importing \\* From a Package"
msgstr "从包中导入 \\*"
#: ../../tutorial/modules.rst:489
msgid ""
"Now what happens when the user writes ``from sound.effects import *``? "
"Ideally, one would hope that this somehow goes out to the filesystem, finds "
"which submodules are present in the package, and imports them all. This "
"could take a long time and importing sub-modules might have unwanted side-"
"effects that should only happen when the sub-module is explicitly imported."
msgstr ""
"使用 ``from sound.effects import *`` "
"时会发生什么?你可能希望它会查找并导入包的所有子模块,但事实并非如此。因为这将花费很长的时间,并且可能会产生你不想要的副作用,如果这种副作用被你设计为只有在导入某个特定的子模块时才应该发生。"
#: ../../tutorial/modules.rst:495
msgid ""
"The only solution is for the package author to provide an explicit index of "
"the package. The :keyword:`import` statement uses the following convention:"
" if a package's :file:`__init__.py` code defines a list named ``__all__``, "
"it is taken to be the list of module names that should be imported when "
"``from package import *`` is encountered. It is up to the package author to"
" keep this list up-to-date when a new version of the package is released. "
"Package authors may also decide not to support it, if they don't see a use "
"for importing \\* from their package. For example, the file "
":file:`sound/effects/__init__.py` could contain the following code::"
msgstr ""
"唯一的解决办法是提供包的显式索引。:keyword:`import` 语句使用如下惯例:如果包的 :file:`__init__.py` 代码定义了列表"
" ``__all__``,运行 ``from package import *`` "
"时,它就是被导入的模块名列表。发布包的新版本时,包的作者应更新此列表。如果包的作者认为没有必要在包中执行导入 \\* "
"操作,也可以不提供此列表。例如,:file:`sound/effects/__init__.py` 文件可以包含以下代码:"
#: ../../tutorial/modules.rst:507
msgid ""
"This would mean that ``from sound.effects import *`` would import the three "
"named submodules of the :mod:`sound.effects` package."
msgstr ""
"这意味着 ``from sound.effects import *`` 将导入 :mod:`sound.effects` 包的以上三个子模块。"
#: ../../tutorial/modules.rst:510
msgid ""
"If ``__all__`` is not defined, the statement ``from sound.effects import *``"
" does *not* import all submodules from the package :mod:`sound.effects` into"
" the current namespace; it only ensures that the package "
":mod:`sound.effects` has been imported (possibly running any initialization "
"code in :file:`__init__.py`) and then imports whatever names are defined in "
"the package. This includes any names defined (and submodules explicitly "
"loaded) by :file:`__init__.py`. It also includes any submodules of the "
"package that were explicitly loaded by previous :keyword:`import` "
"statements. Consider this code::"
msgstr ""
"如果没有定义 ``__all__``,``from sound.effects import *`` 语句 *不会* 把包 "
":mod:`sound.effects` 中所有子模块都导入到当前命名空间;该语句只确保导入包 :mod:`sound.effects` (可能还会运行"
" :file:`__init__.py` 中的初始化代码),然后,再导入包中定义的名称。这些名称包括 :file:`__init__.py` "
"中定义的任何名称(以及显式加载的子模块),还包括之前 :keyword:`import` 语句显式加载的包里的子模块。请看以下代码:"
#: ../../tutorial/modules.rst:523
msgid ""
"In this example, the :mod:`echo` and :mod:`surround` modules are imported in"
" the current namespace because they are defined in the :mod:`sound.effects` "
"package when the ``from...import`` statement is executed. (This also works "
"when ``__all__`` is defined.)"
msgstr ""
"本例中,执行 ``from...import`` 语句时,将把 :mod:`echo` 和 :mod:`surround` "
"模块导入至当前命名空间,因为,它们是在 :mod:`sound.effects` 包里定义的。(该导入操作在定义了 ``__all__`` 时也有效。)"
#: ../../tutorial/modules.rst:528
msgid ""
"Although certain modules are designed to export only names that follow "
"certain patterns when you use ``import *``, it is still considered bad "
"practice in production code."
msgstr "虽然,可以把模块设计为用 ``import *`` 时只导出遵循指定模式的名称,但仍不提倡在生产代码中使用这种做法。"
#: ../../tutorial/modules.rst:532
msgid ""
"Remember, there is nothing wrong with using ``from package import "
"specific_submodule``! In fact, this is the recommended notation unless the "
"importing module needs to use submodules with the same name from different "
"packages."
msgstr ""
"记住,使用 ``from package import specific_submodule`` 没有任何问题! "
"实际上,除了导入模块使用不同包的同名子模块之外,这种方式是推荐用法。"
#: ../../tutorial/modules.rst:539
msgid "Intra-package References"
msgstr "相对导入"
#: ../../tutorial/modules.rst:541
msgid ""
"When packages are structured into subpackages (as with the :mod:`sound` "
"package in the example), you can use absolute imports to refer to submodules"
" of siblings packages. For example, if the module "
":mod:`sound.filters.vocoder` needs to use the :mod:`echo` module in the "
":mod:`sound.effects` package, it can use ``from sound.effects import echo``."
msgstr ""
"包中内有多个子包时(与示例中的 :mod:`sound` 包一样),可以使用绝对导入来引用兄弟包中的子模块。例如,要在模块 "
":mod:`sound.filters.vocoder` 中使用 :mod:`sound.effects` 包的 :mod:`echo` 模块时,可以用"
" ``from sound.effects import echo`` 导入。"
#: ../../tutorial/modules.rst:547
msgid ""
"You can also write relative imports, with the ``from module import name`` "
"form of import statement. These imports use leading dots to indicate the "
"current and parent packages involved in the relative import. From the "
":mod:`surround` module for example, you might use::"
msgstr ""
"还可以用 import 语句的 ``from module import name`` "
"形式执行相对导入。下面的导入语句使用前导句点表示相对导入中的当前包和父包。例如,相对于 :mod:`surround` 模块,可以使用:"
#: ../../tutorial/modules.rst:556
msgid ""
"Note that relative imports are based on the name of the current module. "
"Since the name of the main module is always ``\"__main__\"``, modules "
"intended for use as the main module of a Python application must always use "
"absolute imports."
msgstr ""
"注意,相对导入基于当前模块名。因为主模块名永远是 ``\"__main__\"`` ,所以如果计划将一个模块用作 Python "
"应用程序的主模块,那么该模块内的导入语句必须始终使用绝对导入。"
#: ../../tutorial/modules.rst:562
msgid "Packages in Multiple Directories"
msgstr "多目录中的包"
#: ../../tutorial/modules.rst:564
msgid ""
"Packages support one more special attribute, :attr:`__path__`. This is "
"initialized to be a list containing the name of the directory holding the "
"package's :file:`__init__.py` before the code in that file is executed. "
"This variable can be modified; doing so affects future searches for modules "
"and subpackages contained in the package."
msgstr ""
"包还支持一个特殊属性 :attr:`__path__` 。在包的 :file:`__init__.py` "
"中的代码被执行前,该属性被初始化为一个只含一项的列表,该项是一个字符串,是 __init__.py "
"所在目录的名称。可以修改此变量;这样做会改变在此包中搜索模块和子包的方式。"
#: ../../tutorial/modules.rst:570
msgid ""
"While this feature is not often needed, it can be used to extend the set of "
"modules found in a package."
msgstr "这个功能虽然不常用,但可用于扩展包中的模块集。"
#: ../../tutorial/modules.rst:575
msgid "Footnotes"
msgstr "备注"
#: ../../tutorial/modules.rst:576
msgid ""
"In fact function definitions are also 'statements' that are 'executed'; the "
"execution of a module-level function definition enters the function name in "
"the module's global symbol table."
msgstr "实际上,函数定义也是“可执行”的“语句”;执行模块级函数定义时,函数名将被导入到模块的全局符号表。"