-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathcontrolflow.po
1083 lines (938 loc) · 55 KB
/
controlflow.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
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# 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:
# Rafael Fontenelle <rffontenelle@gmail.com>, 2023
# Freesand Leo <yuqinju@163.com>, 2024
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: Python 3.10\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-05-24 15:56+0000\n"
"PO-Revision-Date: 2022-11-05 17:23+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/controlflow.rst:5
msgid "More Control Flow Tools"
msgstr "更多控制流工具"
#: ../../tutorial/controlflow.rst:7
msgid ""
"Besides the :keyword:`while` statement just introduced, Python uses the "
"usual flow control statements known from other languages, with some twists."
msgstr "除了上一章介绍的 :keyword:`while` 语句,Python 还支持其他语言中常见的流程控制语句,只是稍有不同。"
#: ../../tutorial/controlflow.rst:14
msgid ":keyword:`!if` Statements"
msgstr ":keyword:`!if` 语句"
#: ../../tutorial/controlflow.rst:16
msgid ""
"Perhaps the most well-known statement type is the :keyword:`if` statement. "
"For example::"
msgstr "最让人耳熟能详的语句应当是 :keyword:`if` 语句:"
#: ../../tutorial/controlflow.rst:33
msgid ""
"There can be zero or more :keyword:`elif` parts, and the :keyword:`else` "
"part is optional. The keyword ':keyword:`!elif`' is short for 'else if', "
"and is useful to avoid excessive indentation. An :keyword:`!if` ... "
":keyword:`!elif` ... :keyword:`!elif` ... sequence is a substitute for the "
"``switch`` or ``case`` statements found in other languages."
msgstr ""
"可有零个或多个 :keyword:`elif` 部分,:keyword:`else` 部分也是可选的。关键字 ':keyword:`!elif`' 是 "
"'else if' 的缩写,用于避免过多的缩进。:keyword:`!if` ... :keyword:`!elif` ... "
":keyword:`!elif` ... 序列可以当作其它语言中 ``switch`` 或 ``case`` 语句的替代品。"
#: ../../tutorial/controlflow.rst:39
msgid ""
"If you're comparing the same value to several constants, or checking for "
"specific types or attributes, you may also find the :keyword:`!match` "
"statement useful. For more details see :ref:`tut-match`."
msgstr ""
"如果是把一个值与多个常量进行比较,或者检查特定类型或属性,:keyword:`!match` 语句更有用。详见 :ref:`tut-match`。"
#: ../../tutorial/controlflow.rst:46
msgid ":keyword:`!for` Statements"
msgstr ":keyword:`!for` 语句"
#: ../../tutorial/controlflow.rst:51
msgid ""
"The :keyword:`for` statement in Python differs a bit from what you may be "
"used to in C or Pascal. Rather than always iterating over an arithmetic "
"progression of numbers (like in Pascal), or giving the user the ability to "
"define both the iteration step and halting condition (as C), Python's "
":keyword:`!for` statement iterates over the items of any sequence (a list or"
" a string), in the order that they appear in the sequence. For example (no "
"pun intended):"
msgstr ""
"Python 的 :keyword:`for` 语句与 C 或 Pascal 中的不同。Python 的 :keyword:`!for` "
"语句不迭代算术递增数值(如 Pascal),或是给予用户定义迭代步骤和结束条件的能力(如 "
"C),而是在列表或字符串等任意序列的元素上迭代,按它们在序列中出现的顺序。 例如(这不是有意要暗指什么):"
#: ../../tutorial/controlflow.rst:72
msgid ""
"Code that modifies a collection while iterating over that same collection "
"can be tricky to get right. Instead, it is usually more straight-forward to"
" loop over a copy of the collection or to create a new collection::"
msgstr "很难正确地在迭代多项集的同时修改多项集的内容。更简单的方法是迭代多项集的副本或者创建新的多项集:"
#: ../../tutorial/controlflow.rst:94
msgid "The :func:`range` Function"
msgstr ":func:`range` 函数"
#: ../../tutorial/controlflow.rst:96
msgid ""
"If you do need to iterate over a sequence of numbers, the built-in function "
":func:`range` comes in handy. It generates arithmetic progressions::"
msgstr "内置函数 :func:`range` 用于生成等差数列:"
#: ../../tutorial/controlflow.rst:108
msgid ""
"The given end point is never part of the generated sequence; ``range(10)`` "
"generates 10 values, the legal indices for items of a sequence of length 10."
" It is possible to let the range start at another number, or to specify a "
"different increment (even negative; sometimes this is called the 'step')::"
msgstr ""
"生成的序列绝不会包括给定的终止值;``range(10)`` 生成 10 个值——长度为 10 的序列的所有合法索引。range 可以不从 0 "
"开始,且可以按给定的步长递增(即使是负数步长):"
#: ../../tutorial/controlflow.rst:122
msgid ""
"To iterate over the indices of a sequence, you can combine :func:`range` and"
" :func:`len` as follows::"
msgstr "要按索引迭代序列,可以组合使用 :func:`range` 和 :func:`len`:"
#: ../../tutorial/controlflow.rst:135
msgid ""
"In most such cases, however, it is convenient to use the :func:`enumerate` "
"function, see :ref:`tut-loopidioms`."
msgstr "不过大多数情况下 :func:`enumerate` 函数很方便,详见 :ref:`tut-loopidioms`。"
#: ../../tutorial/controlflow.rst:138
msgid "A strange thing happens if you just print a range::"
msgstr "如果直接打印一个 range 会发生意想不到的事情:"
#: ../../tutorial/controlflow.rst:143
msgid ""
"In many ways the object returned by :func:`range` behaves as if it is a "
"list, but in fact it isn't. It is an object which returns the successive "
"items of the desired sequence when you iterate over it, but it doesn't "
"really make the list, thus saving space."
msgstr ""
":func:`range` "
"返回的对象在很多方面和列表的行为一样,但其实它和列表不一样。该对象只有在被迭代时才一个一个地返回所期望的列表项,并没有真正生成过一个含有全部项的列表,从而节省了空间。"
#: ../../tutorial/controlflow.rst:148
msgid ""
"We say such an object is :term:`iterable`, that is, suitable as a target for"
" functions and constructs that expect something from which they can obtain "
"successive items until the supply is exhausted. We have seen that the "
":keyword:`for` statement is such a construct, while an example of a function"
" that takes an iterable is :func:`sum`::"
msgstr ""
"这种对象称为可迭代对象 :term:`iterable`,适合作为需要获取一系列值的函数或程序构件的参数。:keyword:`for` "
"语句就是这样的程序构件;以可迭代对象作为参数的函数例如 :func:`sum`:"
#: ../../tutorial/controlflow.rst:157
msgid ""
"Later we will see more functions that return iterables and take iterables as"
" arguments. In chapter :ref:`tut-structures`, we will discuss in more "
"detail about :func:`list`."
msgstr ""
"之后我们会看到更多返回可迭代对象,或以可迭代对象作为参数的函数。在 :ref:`tut-structures` 这一章中,我们将讨论 "
":func:`list` 的更多细节。"
#: ../../tutorial/controlflow.rst:164
msgid ""
":keyword:`!break` and :keyword:`!continue` Statements, and :keyword:`!else` "
"Clauses on Loops"
msgstr "循环中的 :keyword:`!break`、:keyword:`!continue` 语句及 :keyword:`!else` 子句"
#: ../../tutorial/controlflow.rst:166
msgid ""
"The :keyword:`break` statement, like in C, breaks out of the innermost "
"enclosing :keyword:`for` or :keyword:`while` loop."
msgstr ""
":keyword:`break` 语句和 C 中的类似,用于跳出最近的 :keyword:`for` 或 :keyword:`while` 循环。"
#: ../../tutorial/controlflow.rst:169
msgid ""
"Loop statements may have an :keyword:`!else` clause; it is executed when the"
" loop terminates through exhaustion of the iterable (with :keyword:`for`) or"
" when the condition becomes false (with :keyword:`while`), but not when the "
"loop is terminated by a :keyword:`break` statement. This is exemplified by "
"the following loop, which searches for prime numbers::"
msgstr ""
"循环语句支持 :keyword:`!else` 子句;:keyword:`for` 循环中,可迭代对象中的元素全部循环完毕,或 "
":keyword:`while` 循环的条件为假时,执行该子句;:keyword:`break` 语句终止循环时,不执行该子句。 "
"请看下面这个查找素数的循环示例:"
#: ../../tutorial/controlflow.rst:193
msgid ""
"(Yes, this is the correct code. Look closely: the ``else`` clause belongs "
"to the :keyword:`for` loop, **not** the :keyword:`if` statement.)"
msgstr ""
"(没错,这段代码就是这么写。仔细看:``else`` 子句属于 :keyword:`for` 循环,**不属于** :keyword:`if` 语句。)"
#: ../../tutorial/controlflow.rst:196
msgid ""
"When used with a loop, the ``else`` clause has more in common with the "
"``else`` clause of a :keyword:`try` statement than it does with that of "
":keyword:`if` statements: a :keyword:`try` statement's ``else`` clause runs "
"when no exception occurs, and a loop's ``else`` clause runs when no "
"``break`` occurs. For more on the :keyword:`!try` statement and exceptions, "
"see :ref:`tut-handling`."
msgstr ""
"``else`` 子句用于循环时比起 :keyword:`if` 语句的 ``else`` 子句,更像 :keyword:`try` "
"语句的。:keyword:`try` 语句的 ``else`` 子句在未发生异常时执行,循环的 ``else`` 子句则在未发生 ``break`` "
"时执行。 :keyword:`!try` 语句和异常详见 :ref:`tut-handling`。"
#: ../../tutorial/controlflow.rst:203
msgid ""
"The :keyword:`continue` statement, also borrowed from C, continues with the "
"next iteration of the loop::"
msgstr ":keyword:`continue` 语句,同样借鉴自 C 语言,以执行循环的下一次迭代来继续:"
#: ../../tutorial/controlflow.rst:224
msgid ":keyword:`!pass` Statements"
msgstr ":keyword:`!pass` 语句"
#: ../../tutorial/controlflow.rst:226
msgid ""
"The :keyword:`pass` statement does nothing. It can be used when a statement "
"is required syntactically but the program requires no action. For example::"
msgstr ":keyword:`pass` 语句不执行任何动作。语法上需要一个语句,但程序毋需执行任何动作时,可以使用该语句。例如:"
#: ../../tutorial/controlflow.rst:233
msgid "This is commonly used for creating minimal classes::"
msgstr "这常用于创建一个最小的类:"
#: ../../tutorial/controlflow.rst:239
msgid ""
"Another place :keyword:`pass` can be used is as a place-holder for a "
"function or conditional body when you are working on new code, allowing you "
"to keep thinking at a more abstract level. The :keyword:`!pass` is silently"
" ignored::"
msgstr ""
":keyword:`pass` 还可用作函数或条件语句体的占位符,让你保持在更抽象的层次进行思考。:keyword:`!pass` 会被默默地忽略:"
#: ../../tutorial/controlflow.rst:251
msgid ":keyword:`!match` Statements"
msgstr ":keyword:`!match` 语句"
#: ../../tutorial/controlflow.rst:253
msgid ""
"A :keyword:`match` statement takes an expression and compares its value to "
"successive patterns given as one or more case blocks. This is superficially"
" similar to a switch statement in C, Java or JavaScript (and many other "
"languages), but it's more similar to pattern matching in languages like Rust"
" or Haskell. Only the first pattern that matches gets executed and it can "
"also extract components (sequence elements or object attributes) from the "
"value into variables."
msgstr ""
":keyword:`match` 语句接受一个表达式并把它的值与一个或多个 case 块给出的一系列模式进行比较。这表面上像 C、Java 或 "
"JavaScript(以及许多其他程序设计语言)中的 switch 语句,但其实它更像 Rust 或 Haskell "
"中的模式匹配。只有第一个匹配的模式会被执行,并且它还可以提取值的组成部分(序列的元素或对象的属性)赋给变量。"
#: ../../tutorial/controlflow.rst:261
msgid ""
"The simplest form compares a subject value against one or more literals::"
msgstr "最简单的形式是将一个主语值与一个或多个字面值进行比较:"
#: ../../tutorial/controlflow.rst:274
msgid ""
"Note the last block: the \"variable name\" ``_`` acts as a *wildcard* and "
"never fails to match. If no case matches, none of the branches is executed."
msgstr "注意最后一个代码块:“变量名” ``_`` 被作为 *通配符* 并必定会匹配成功。如果没有 case 匹配成功,则不会执行任何分支。"
#: ../../tutorial/controlflow.rst:277
msgid ""
"You can combine several literals in a single pattern using ``|`` (\"or\")::"
msgstr "你可以使用 ``|`` (“ or ”)在一个模式中组合几个字面值::"
#: ../../tutorial/controlflow.rst:282
msgid ""
"Patterns can look like unpacking assignments, and can be used to bind "
"variables::"
msgstr "形如解包赋值的模式可被用于绑定变量:"
#: ../../tutorial/controlflow.rst:298
msgid ""
"Study that one carefully! The first pattern has two literals, and can be "
"thought of as an extension of the literal pattern shown above. But the next"
" two patterns combine a literal and a variable, and the variable *binds* a "
"value from the subject (``point``). The fourth pattern captures two values,"
" which makes it conceptually similar to the unpacking assignment ``(x, y) = "
"point``."
msgstr ""
"请仔细学习此代码!第一个模式有两个字面值,可视为前述字面值模式的扩展。接下来的两个模式结合了一个字面值和一个变量,变量 *绑定* "
"了来自主语(``point``)的一个值。第四个模式捕获了两个值,使其在概念上与解包赋值 ``(x, y) = point`` 类似。"
#: ../../tutorial/controlflow.rst:305
msgid ""
"If you are using classes to structure your data you can use the class name "
"followed by an argument list resembling a constructor, but with the ability "
"to capture attributes into variables::"
msgstr "如果用类组织数据,可以用“类名后接一个参数列表”这种很像构造器的形式,把属性捕获到变量里:"
#: ../../tutorial/controlflow.rst:326
msgid ""
"You can use positional parameters with some builtin classes that provide an "
"ordering for their attributes (e.g. dataclasses). You can also define a "
"specific position for attributes in patterns by setting the "
"``__match_args__`` special attribute in your classes. If it's set to (\"x\","
" \"y\"), the following patterns are all equivalent (and all bind the ``y`` "
"attribute to the ``var`` variable)::"
msgstr ""
"你可以在某些为其属性提供了排序的内置类(例如 dataclass)中使用位置参数。 你也可以通过在你的类中设置 ``__match_args__`` "
"特殊属性来为模式中的属性定义一个专门的位置。 如果它被设为 (\"x\", \"y\"),则以下模式均为等价的(并且都是将 ``y`` 属性绑定到 "
"``var`` 变量)::"
#: ../../tutorial/controlflow.rst:337
msgid ""
"A recommended way to read patterns is to look at them as an extended form of"
" what you would put on the left of an assignment, to understand which "
"variables would be set to what. Only the standalone names (like ``var`` "
"above) are assigned to by a match statement. Dotted names (like "
"``foo.bar``), attribute names (the ``x=`` and ``y=`` above) or class names "
"(recognized by the \"(...)\" next to them like ``Point`` above) are never "
"assigned to."
msgstr ""
"建议这样来阅读一个模式——通过将其视为赋值语句等号左边的一种扩展形式,来理解各个变量被设为何值。match 语句只会为单一的名称(如上面的 "
"``var``)赋值,而不会赋值给带点号的名称(如 ``foo.bar``)、属性名(如上面的 ``x=`` 和 ``y=``)和类名(是通过其后的 "
"\"(...)\" 来识别的,如上面的 ``Point``)。"
#: ../../tutorial/controlflow.rst:344
msgid ""
"Patterns can be arbitrarily nested. For example, if we have a short list of"
" points, we could match it like this::"
msgstr "模式可以任意地嵌套。例如,如果有一个由点组成的短列表,则可使用如下方式进行匹配:"
#: ../../tutorial/controlflow.rst:359
msgid ""
"We can add an ``if`` clause to a pattern, known as a \"guard\". If the "
"guard is false, ``match`` goes on to try the next case block. Note that "
"value capture happens before the guard is evaluated::"
msgstr ""
"我们可以向一个模式添加 ``if`` 子句,称为“约束项”。 如果约束项为假值,则 ``match`` 将继续尝试下一个 case 语句块。 "
"请注意值的捕获发生在约束项被求值之前。::"
#: ../../tutorial/controlflow.rst:369
msgid "Several other key features of this statement:"
msgstr "该语句的一些其它关键特性:"
#: ../../tutorial/controlflow.rst:371
msgid ""
"Like unpacking assignments, tuple and list patterns have exactly the same "
"meaning and actually match arbitrary sequences. An important exception is "
"that they don't match iterators or strings."
msgstr "与解包赋值类似,元组和列表模式具有完全相同的含义并且实际上都能匹配任意序列,区别是它们不能匹配迭代器或字符串。"
#: ../../tutorial/controlflow.rst:375
msgid ""
"Sequence patterns support extended unpacking: ``[x, y, *rest]`` and ``(x, y,"
" *rest)`` work similar to unpacking assignments. The name after ``*`` may "
"also be ``_``, so ``(x, y, *_)`` matches a sequence of at least two items "
"without binding the remaining items."
msgstr ""
"序列模式支持扩展解包:``[x, y, *rest]`` 和 ``(x, y, *rest)`` 和相应的解包赋值做的事是一样的。接在 ``*`` "
"后的名称也可以为 ``_``,所以 ``(x, y, *_)`` 匹配含至少两项的序列,而不必绑定剩余的项。"
#: ../../tutorial/controlflow.rst:380
msgid ""
"Mapping patterns: ``{\"bandwidth\": b, \"latency\": l}`` captures the "
"``\"bandwidth\"`` and ``\"latency\"`` values from a dictionary. Unlike "
"sequence patterns, extra keys are ignored. An unpacking like ``**rest`` is "
"also supported. (But ``**_`` would be redundant, so it is not allowed.)"
msgstr ""
"映射模式:``{\"bandwidth\": b, \"latency\": l}`` 从字典中捕获 ``\"bandwidth\"`` 和 "
"``\"latency\"`` 的值。额外的键会被忽略,这一点与序列模式不同。``**rest`` 这样的解包也支持。(但 ``**_`` "
"将会是冗余的,故不允许使用。)"
#: ../../tutorial/controlflow.rst:385
msgid "Subpatterns may be captured using the ``as`` keyword::"
msgstr "子模式可使用 ``as`` 关键字来捕获::"
#: ../../tutorial/controlflow.rst:389
msgid ""
"will capture the second element of the input as ``p2`` (as long as the input"
" is a sequence of two points)"
msgstr "将把输入中的第二个元素捕获为 ``p2`` (只要输入是包含两个点的序列)"
#: ../../tutorial/controlflow.rst:392
msgid ""
"Most literals are compared by equality, however the singletons ``True``, "
"``False`` and ``None`` are compared by identity."
msgstr "大多数字面值是按相等性比较的,但是单例对象 ``True``、``False`` 和 ``None`` 则是按 id 比较的。"
#: ../../tutorial/controlflow.rst:395
msgid ""
"Patterns may use named constants. These must be dotted names to prevent "
"them from being interpreted as capture variable::"
msgstr "模式可以使用具名常量。它们必须作为带点号的名称出现,以防止它们被解释为用于捕获的变量:"
#: ../../tutorial/controlflow.rst:414
msgid ""
"For a more detailed explanation and additional examples, you can look into "
":pep:`636` which is written in a tutorial format."
msgstr "更详细的说明和更多示例,可参阅以教程格式撰写的 :pep:`636`。"
#: ../../tutorial/controlflow.rst:420
msgid "Defining Functions"
msgstr "定义函数"
#: ../../tutorial/controlflow.rst:422
msgid ""
"We can create a function that writes the Fibonacci series to an arbitrary "
"boundary::"
msgstr "下列代码创建一个可以输出限定数值内的斐波那契数列函数:"
#: ../../tutorial/controlflow.rst:442
msgid ""
"The keyword :keyword:`def` introduces a function *definition*. It must be "
"followed by the function name and the parenthesized list of formal "
"parameters. The statements that form the body of the function start at the "
"next line, and must be indented."
msgstr "*定义* 函数使用关键字 :keyword:`def`,后跟函数名与括号内的形参列表。函数语句从下一行开始,并且必须缩进。"
#: ../../tutorial/controlflow.rst:447
msgid ""
"The first statement of the function body can optionally be a string literal;"
" this string literal is the function's documentation string, or "
":dfn:`docstring`. (More about docstrings can be found in the section "
":ref:`tut-docstrings`.) There are tools which use docstrings to "
"automatically produce online or printed documentation, or to let the user "
"interactively browse through code; it's good practice to include docstrings "
"in code that you write, so make a habit of it."
msgstr ""
"函数内的第一条语句是字符串时,该字符串就是文档字符串,也称为 :dfn:`docstring`,详见 :ref:`tut-"
"docstrings`。利用文档字符串可以自动生成在线文档或打印版文档,还可以让开发者在浏览代码时直接查阅文档;Python "
"开发者最好养成在代码中加入文档字符串的好习惯。"
#: ../../tutorial/controlflow.rst:454
msgid ""
"The *execution* of a function introduces a new symbol table used for the "
"local variables of the function. More precisely, all variable assignments "
"in a function store the value in the local symbol table; whereas variable "
"references first look in the local symbol table, then in the local symbol "
"tables of enclosing functions, then in the global symbol table, and finally "
"in the table of built-in names. Thus, global variables and variables of "
"enclosing functions cannot be directly assigned a value within a function "
"(unless, for global variables, named in a :keyword:`global` statement, or, "
"for variables of enclosing functions, named in a :keyword:`nonlocal` "
"statement), although they may be referenced."
msgstr ""
"函数在 *执行* "
"时使用函数局部变量符号表,所有函数变量赋值都存在局部符号表中;引用变量时,首先,在局部符号表里查找变量,然后,是外层函数局部符号表,再是全局符号表,最后是内置名称符号表。因此,尽管可以引用全局变量和外层函数的变量,但最好不要在函数内直接赋值(除非是"
" :keyword:`global` 语句定义的全局变量,或 :keyword:`nonlocal` 语句定义的外层函数变量)。"
#: ../../tutorial/controlflow.rst:465
msgid ""
"The actual parameters (arguments) to a function call are introduced in the "
"local symbol table of the called function when it is called; thus, arguments"
" are passed using *call by value* (where the *value* is always an object "
"*reference*, not the value of the object). [#]_ When a function calls "
"another function, or calls itself recursively, a new local symbol table is "
"created for that call."
msgstr ""
"在调用函数时会将实际参数(实参)引入到被调用函数的局部符号表中;因此,实参是使用 *按值调用* 来传递的(其中的 *值* 始终是对象的 *引用* "
"而不是对象的值)。 [#]_ 当一个函数调用另外一个函数时,会为该调用创建一个新的局部符号表。"
#: ../../tutorial/controlflow.rst:472
msgid ""
"A function definition associates the function name with the function object "
"in the current symbol table. The interpreter recognizes the object pointed "
"to by that name as a user-defined function. Other names can also point to "
"that same function object and can also be used to access the function::"
msgstr ""
"函数定义在当前符号表中把函数名与函数对象关联在一起。解释器把函数名指向的对象作为用户自定义函数。还可以使用其他名称指向同一个函数对象,并访问访该函数:"
#: ../../tutorial/controlflow.rst:483
msgid ""
"Coming from other languages, you might object that ``fib`` is not a function"
" but a procedure since it doesn't return a value. In fact, even functions "
"without a :keyword:`return` statement do return a value, albeit a rather "
"boring one. This value is called ``None`` (it's a built-in name). Writing "
"the value ``None`` is normally suppressed by the interpreter if it would be "
"the only value written. You can see it if you really want to using "
":func:`print`::"
msgstr ""
"如果你用过其他语言,你可能会认为 ``fib`` 不是函数而是一个过程,因为它没有返回值。 事实上,即使没有 :keyword:`return` "
"语句的函数也有返回值,尽管这个值可能相当无聊。 这个值被称为 ``None`` (是一个内置名称)。 通常解释器会屏蔽单独的返回值 ``None``。 "
"如果你确有需要可以使用 :func:`print` 查看它::"
#: ../../tutorial/controlflow.rst:494
msgid ""
"It is simple to write a function that returns a list of the numbers of the "
"Fibonacci series, instead of printing it::"
msgstr "编写不直接输出斐波那契数列运算结果,而是返回运算结果列表的函数也非常简单:"
#: ../../tutorial/controlflow.rst:510
msgid "This example, as usual, demonstrates some new Python features:"
msgstr "本例也新引入了一些 Python 功能:"
#: ../../tutorial/controlflow.rst:512
msgid ""
"The :keyword:`return` statement returns with a value from a function. "
":keyword:`!return` without an expression argument returns ``None``. Falling "
"off the end of a function also returns ``None``."
msgstr ""
":keyword:`return` 语句返回函数的值。:keyword:`!return` 语句不带表达式参数时,返回 "
"``None``。函数执行完毕退出也返回 ``None``。"
#: ../../tutorial/controlflow.rst:516
msgid ""
"The statement ``result.append(a)`` calls a *method* of the list object "
"``result``. A method is a function that 'belongs' to an object and is named"
" ``obj.methodname``, where ``obj`` is some object (this may be an "
"expression), and ``methodname`` is the name of a method that is defined by "
"the object's type. Different types define different methods. Methods of "
"different types may have the same name without causing ambiguity. (It is "
"possible to define your own object types and methods, using *classes*, see "
":ref:`tut-classes`) The method :meth:`append` shown in the example is "
"defined for list objects; it adds a new element at the end of the list. In "
"this example it is equivalent to ``result = result + [a]``, but more "
"efficient."
msgstr ""
"``result.append(a)`` 语句调用了列表对象 ``result`` 的 *方法* 。方法是“从属于”对象的函数,命名为 "
"``obj.methodname``,``obj`` 是对象(也可以是表达式),``methodname`` "
"是对象类型定义的方法名。不同类型定义不同的方法,不同类型的方法名可以相同,且不会引起歧义。(用 *类* 可以自定义对象类型和方法,详见 "
":ref:`tut-classes` )示例中的方法 :meth:`append` 是为列表对象定义的,用于在列表末尾添加新元素。本例中,该方法相当于 "
"``result = result + [a]`` ,但更有效。"
#: ../../tutorial/controlflow.rst:531
msgid "More on Defining Functions"
msgstr "函数定义详解"
#: ../../tutorial/controlflow.rst:533
msgid ""
"It is also possible to define functions with a variable number of arguments."
" There are three forms, which can be combined."
msgstr "函数定义支持可变数量的参数。这里列出三种可以组合使用的形式。"
#: ../../tutorial/controlflow.rst:540
msgid "Default Argument Values"
msgstr "默认值参数"
#: ../../tutorial/controlflow.rst:542
msgid ""
"The most useful form is to specify a default value for one or more "
"arguments. This creates a function that can be called with fewer arguments "
"than it is defined to allow. For example::"
msgstr "为参数指定默认值是非常有用的方式。调用函数时,可以使用比定义时更少的参数,例如:"
#: ../../tutorial/controlflow.rst:558
msgid "This function can be called in several ways:"
msgstr "该函数可以用以下方式调用:"
#: ../../tutorial/controlflow.rst:560
msgid ""
"giving only the mandatory argument: ``ask_ok('Do you really want to "
"quit?')``"
msgstr "只给出必选实参:``ask_ok('Do you really want to quit?')``"
#: ../../tutorial/controlflow.rst:562
msgid ""
"giving one of the optional arguments: ``ask_ok('OK to overwrite the file?', "
"2)``"
msgstr "给出一个可选实参:``ask_ok('OK to overwrite the file?', 2)``"
#: ../../tutorial/controlflow.rst:564
msgid ""
"or even giving all arguments: ``ask_ok('OK to overwrite the file?', 2, 'Come"
" on, only yes or no!')``"
msgstr ""
"给出所有实参:``ask_ok('OK to overwrite the file?', 2, 'Come on, only yes or "
"no!')``"
#: ../../tutorial/controlflow.rst:567
msgid ""
"This example also introduces the :keyword:`in` keyword. This tests whether "
"or not a sequence contains a certain value."
msgstr "本例还使用了关键字 :keyword:`in` ,用于确认序列中是否包含某个值。"
#: ../../tutorial/controlflow.rst:570
msgid ""
"The default values are evaluated at the point of function definition in the "
"*defining* scope, so that ::"
msgstr "默认值在 *定义* 作用域里的函数定义中求值,所以:"
#: ../../tutorial/controlflow.rst:581
msgid "will print ``5``."
msgstr "上例输出的是 ``5``。"
#: ../../tutorial/controlflow.rst:583
msgid ""
"**Important warning:** The default value is evaluated only once. This makes"
" a difference when the default is a mutable object such as a list, "
"dictionary, or instances of most classes. For example, the following "
"function accumulates the arguments passed to it on subsequent calls::"
msgstr ""
"**重要警告:** 默认值只计算一次。默认值为列表、字典或类实例等可变对象时,会产生与该规则不同的结果。例如,下面的函数会累积后续调用时传递的参数:"
#: ../../tutorial/controlflow.rst:596
msgid "This will print ::"
msgstr "输出结果如下:"
#: ../../tutorial/controlflow.rst:602
msgid ""
"If you don't want the default to be shared between subsequent calls, you can"
" write the function like this instead::"
msgstr "不想在后续调用之间共享默认值时,应以如下方式编写函数:"
#: ../../tutorial/controlflow.rst:615
msgid "Keyword Arguments"
msgstr "关键字参数"
#: ../../tutorial/controlflow.rst:617
msgid ""
"Functions can also be called using :term:`keyword arguments <keyword "
"argument>` of the form ``kwarg=value``. For instance, the following "
"function::"
msgstr ""
"``kwarg=value`` 形式的 :term:`关键字参数 <keyword argument>` 也可以用于调用函数。函数示例如下:"
#: ../../tutorial/controlflow.rst:626
msgid ""
"accepts one required argument (``voltage``) and three optional arguments "
"(``state``, ``action``, and ``type``). This function can be called in any "
"of the following ways::"
msgstr ""
"该函数接受一个必选参数(``voltage``)和三个可选参数(``state``, ``action`` 和 "
"``type``)。该函数可用下列方式调用:"
#: ../../tutorial/controlflow.rst:637
msgid "but all the following calls would be invalid::"
msgstr "以下调用函数的方式都无效:"
#: ../../tutorial/controlflow.rst:644
msgid ""
"In a function call, keyword arguments must follow positional arguments. All "
"the keyword arguments passed must match one of the arguments accepted by the"
" function (e.g. ``actor`` is not a valid argument for the ``parrot`` "
"function), and their order is not important. This also includes non-"
"optional arguments (e.g. ``parrot(voltage=1000)`` is valid too). No argument"
" may receive a value more than once. Here's an example that fails due to "
"this restriction::"
msgstr ""
"函数调用时,关键字参数必须跟在位置参数后面。所有传递的关键字参数都必须匹配一个函数接受的参数(比如,``actor`` 不是函数 ``parrot`` "
"的有效参数),关键字参数的顺序并不重要。这也包括必选参数,(比如,``parrot(voltage=1000)`` "
"也有效)。不能对同一个参数多次赋值,下面就是一个因此限制而失败的例子:"
#: ../../tutorial/controlflow.rst:660
msgid ""
"When a final formal parameter of the form ``**name`` is present, it receives"
" a dictionary (see :ref:`typesmapping`) containing all keyword arguments "
"except for those corresponding to a formal parameter. This may be combined "
"with a formal parameter of the form ``*name`` (described in the next "
"subsection) which receives a :ref:`tuple <tut-tuples>` containing the "
"positional arguments beyond the formal parameter list. (``*name`` must "
"occur before ``**name``.) For example, if we define a function like this::"
msgstr ""
"最后一个形参为 ``**name`` 形式时,接收一个字典(详见 "
":ref:`typesmapping`),该字典包含与函数中已定义形参对应之外的所有关键字参数。``**name`` 形参可以与 ``*name`` "
"形参(下一小节介绍)组合使用(``*name`` 必须在 ``**name`` 前面), ``*name`` 形参接收一个 :ref:`元组 <tut-"
"tuples>`,该元组包含形参列表之外的位置参数。例如,可以定义下面这样的函数:"
#: ../../tutorial/controlflow.rst:677
msgid "It could be called like this::"
msgstr "该函数可以用如下方式调用:"
#: ../../tutorial/controlflow.rst:685
msgid "and of course it would print:"
msgstr "输出结果如下:"
#: ../../tutorial/controlflow.rst:698
msgid ""
"Note that the order in which the keyword arguments are printed is guaranteed"
" to match the order in which they were provided in the function call."
msgstr "注意,关键字参数在输出结果中的顺序与调用函数时的顺序一致。"
#: ../../tutorial/controlflow.rst:702
msgid "Special parameters"
msgstr "特殊参数"
#: ../../tutorial/controlflow.rst:704
msgid ""
"By default, arguments may be passed to a Python function either by position "
"or explicitly by keyword. For readability and performance, it makes sense to"
" restrict the way arguments can be passed so that a developer need only look"
" at the function definition to determine if items are passed by position, by"
" position or keyword, or by keyword."
msgstr ""
"默认情况下,参数可以按位置或显式关键字传递给 Python "
"函数。为了让代码易读、高效,最好限制参数的传递方式,这样,开发者只需查看函数定义,即可确定参数项是仅按位置、按位置或关键字,还是仅按关键字传递。"
#: ../../tutorial/controlflow.rst:710
msgid "A function definition may look like:"
msgstr "函数定义如下:"
#: ../../tutorial/controlflow.rst:721
msgid ""
"where ``/`` and ``*`` are optional. If used, these symbols indicate the kind"
" of parameter by how the arguments may be passed to the function: "
"positional-only, positional-or-keyword, and keyword-only. Keyword parameters"
" are also referred to as named parameters."
msgstr "``/`` 和 ``*`` 是可选的。这些符号表明形参如何把参数值传递给函数:位置、位置或关键字、关键字。关键字形参也叫作命名形参。"
#: ../../tutorial/controlflow.rst:728
msgid "Positional-or-Keyword Arguments"
msgstr "位置或关键字参数"
#: ../../tutorial/controlflow.rst:730
msgid ""
"If ``/`` and ``*`` are not present in the function definition, arguments may"
" be passed to a function by position or by keyword."
msgstr "函数定义中未使用 ``/`` 和 ``*`` 时,参数可以按位置或关键字传递给函数。"
#: ../../tutorial/controlflow.rst:735
msgid "Positional-Only Parameters"
msgstr "仅位置参数"
#: ../../tutorial/controlflow.rst:737
msgid ""
"Looking at this in a bit more detail, it is possible to mark certain "
"parameters as *positional-only*. If *positional-only*, the parameters' order"
" matters, and the parameters cannot be passed by keyword. Positional-only "
"parameters are placed before a ``/`` (forward-slash). The ``/`` is used to "
"logically separate the positional-only parameters from the rest of the "
"parameters. If there is no ``/`` in the function definition, there are no "
"positional-only parameters."
msgstr ""
"此处再介绍一些细节,特定形参可以标记为 *仅限位置*。*仅限位置* 时,形参的顺序很重要,且这些形参不能用关键字传递。仅限位置形参应放在 ``/`` "
"(正斜杠)前。``/`` 用于在逻辑上分割仅限位置形参与其它形参。如果函数定义中没有 ``/``,则表示没有仅限位置形参。"
#: ../../tutorial/controlflow.rst:745
msgid ""
"Parameters following the ``/`` may be *positional-or-keyword* or *keyword-"
"only*."
msgstr "``/`` 后可以是 *位置或关键字* 或 *仅限关键字* 形参。"
#: ../../tutorial/controlflow.rst:749
msgid "Keyword-Only Arguments"
msgstr "仅限关键字参数"
#: ../../tutorial/controlflow.rst:751
msgid ""
"To mark parameters as *keyword-only*, indicating the parameters must be "
"passed by keyword argument, place an ``*`` in the arguments list just before"
" the first *keyword-only* parameter."
msgstr "把形参标记为 *仅限关键字*,表明必须以关键字参数形式传递该形参,应在参数列表中第一个 *仅限关键字* 形参前添加 ``*``。"
#: ../../tutorial/controlflow.rst:757
msgid "Function Examples"
msgstr "函数示例"
#: ../../tutorial/controlflow.rst:759
msgid ""
"Consider the following example function definitions paying close attention "
"to the markers ``/`` and ``*``::"
msgstr "请看下面的函数定义示例,注意 ``/`` 和 ``*`` 标记:"
#: ../../tutorial/controlflow.rst:775
msgid ""
"The first function definition, ``standard_arg``, the most familiar form, "
"places no restrictions on the calling convention and arguments may be passed"
" by position or keyword::"
msgstr "第一个函数定义 ``standard_arg`` 是最常见的形式,对调用方式没有任何限制,可以按位置也可以按关键字传递参数:"
#: ../../tutorial/controlflow.rst:785
msgid ""
"The second function ``pos_only_arg`` is restricted to only use positional "
"parameters as there is a ``/`` in the function definition::"
msgstr "第二个函数 ``pos_only_arg`` 的函数定义中有 ``/``,仅限使用位置形参:"
#: ../../tutorial/controlflow.rst:796
msgid ""
"The third function ``kwd_only_args`` only allows keyword arguments as "
"indicated by a ``*`` in the function definition::"
msgstr "第三个函数 ``kwd_only_args`` 的函数定义通过 ``*`` 表明仅限关键字参数:"
#: ../../tutorial/controlflow.rst:807
msgid ""
"And the last uses all three calling conventions in the same function "
"definition::"
msgstr "最后一个函数在同一个函数定义中,使用了全部三种调用惯例:"
#: ../../tutorial/controlflow.rst:827
msgid ""
"Finally, consider this function definition which has a potential collision "
"between the positional argument ``name`` and ``**kwds`` which has ``name`` "
"as a key::"
msgstr "下面的函数定义中,``kwds`` 把 ``name`` 当作键,因此,可能与位置参数 ``name`` 产生潜在冲突:"
#: ../../tutorial/controlflow.rst:832
msgid ""
"There is no possible call that will make it return ``True`` as the keyword "
"``'name'`` will always bind to the first parameter. For example::"
msgstr "调用该函数不可能返回 ``True``,因为关键字 ``'name'`` 总与第一个形参绑定。例如:"
#: ../../tutorial/controlflow.rst:841
msgid ""
"But using ``/`` (positional only arguments), it is possible since it allows "
"``name`` as a positional argument and ``'name'`` as a key in the keyword "
"arguments::"
msgstr ""
"加上 ``/`` (仅限位置参数)后,就可以了。此时,函数定义把 ``name`` 当作位置参数,``'name'`` 也可以作为关键字参数的键:"
#: ../../tutorial/controlflow.rst:848
msgid ""
"In other words, the names of positional-only parameters can be used in "
"``**kwds`` without ambiguity."
msgstr "换句话说,仅限位置形参的名称可以在 ``**kwds`` 中使用,而不产生歧义。"
#: ../../tutorial/controlflow.rst:853
msgid "Recap"
msgstr "小结"
#: ../../tutorial/controlflow.rst:855
msgid ""
"The use case will determine which parameters to use in the function "
"definition::"
msgstr "以下用例决定哪些形参可以用于函数定义:"
#: ../../tutorial/controlflow.rst:859
msgid "As guidance:"
msgstr "说明:"
#: ../../tutorial/controlflow.rst:861
msgid ""
"Use positional-only if you want the name of the parameters to not be "
"available to the user. This is useful when parameter names have no real "
"meaning, if you want to enforce the order of the arguments when the function"
" is called or if you need to take some positional parameters and arbitrary "
"keywords."
msgstr "使用仅限位置形参,可以让用户无法使用形参名。形参名没有实际意义时,强制调用函数的实参顺序时,或同时接收位置形参和关键字时,这种方式很有用。"
#: ../../tutorial/controlflow.rst:866
msgid ""
"Use keyword-only when names have meaning and the function definition is more"
" understandable by being explicit with names or you want to prevent users "
"relying on the position of the argument being passed."
msgstr "当形参名有实际意义,且显式名称可以让函数定义更易理解时,阻止用户依赖传递实参的位置时,才使用关键字。"
#: ../../tutorial/controlflow.rst:869
msgid ""
"For an API, use positional-only to prevent breaking API changes if the "
"parameter's name is modified in the future."
msgstr "对于 API,使用仅限位置形参,可以防止未来修改形参名时造成破坏性的 API 变动。"
#: ../../tutorial/controlflow.rst:875
msgid "Arbitrary Argument Lists"
msgstr "任意实参列表"
#: ../../tutorial/controlflow.rst:880
msgid ""
"Finally, the least frequently used option is to specify that a function can "
"be called with an arbitrary number of arguments. These arguments will be "
"wrapped up in a tuple (see :ref:`tut-tuples`). Before the variable number "
"of arguments, zero or more normal arguments may occur. ::"
msgstr ""
"调用函数时,使用任意数量的实参是最少见的选项。这些实参包含在元组中(详见 :ref:`tut-tuples` "
")。在可变数量的实参之前,可能有若干个普通参数:"
#: ../../tutorial/controlflow.rst:889
msgid ""
"Normally, these *variadic* arguments will be last in the list of formal "
"parameters, because they scoop up all remaining input arguments that are "
"passed to the function. Any formal parameters which occur after the "
"``*args`` parameter are 'keyword-only' arguments, meaning that they can only"
" be used as keywords rather than positional arguments. ::"
msgstr ""
"*variadic* 参数用于采集传递给函数的所有剩余参数,因此,它们通常在形参列表的末尾。``*args`` "
"形参后的任何形式参数只能是仅限关键字参数,即只能用作关键字参数,不能用作位置参数:"
#: ../../tutorial/controlflow.rst:906
msgid "Unpacking Argument Lists"
msgstr "解包实参列表"
#: ../../tutorial/controlflow.rst:908
msgid ""
"The reverse situation occurs when the arguments are already in a list or "
"tuple but need to be unpacked for a function call requiring separate "
"positional arguments. For instance, the built-in :func:`range` function "
"expects separate *start* and *stop* arguments. If they are not available "
"separately, write the function call with the ``*``\\ -operator to unpack "
"the arguments out of a list or tuple::"
msgstr ""
"函数调用要求独立的位置参数,但实参在列表或元组里时,要执行相反的操作。例如,内置的 :func:`range` 函数要求独立的 *start* 和 "
"*stop* 实参。如果这些参数不是独立的,则要在调用函数时,用 ``*`` 操作符把实参从列表或元组解包出来:"
#: ../../tutorial/controlflow.rst:924
msgid ""
"In the same fashion, dictionaries can deliver keyword arguments with the "
"``**``\\ -operator::"
msgstr "同样,字典可以用 ``**`` 操作符传递关键字参数:"
#: ../../tutorial/controlflow.rst:940
msgid "Lambda Expressions"
msgstr "Lambda 表达式"
#: ../../tutorial/controlflow.rst:942
msgid ""
"Small anonymous functions can be created with the :keyword:`lambda` keyword."
" This function returns the sum of its two arguments: ``lambda a, b: a+b``. "
"Lambda functions can be used wherever function objects are required. They "
"are syntactically restricted to a single expression. Semantically, they are"
" just syntactic sugar for a normal function definition. Like nested "
"function definitions, lambda functions can reference variables from the "
"containing scope::"
msgstr ""
":keyword:`lambda` 关键字用于创建小巧的匿名函数。``lambda a, b: a+b`` 函数返回两个参数的和。Lambda "
"函数可用于任何需要函数对象的地方。在语法上,匿名函数只能是单个表达式。在语义上,它只是常规函数定义的语法糖。与嵌套函数定义一样,lambda "
"函数可以引用包含作用域中的变量:"
#: ../../tutorial/controlflow.rst:959
msgid ""
"The above example uses a lambda expression to return a function. Another "
"use is to pass a small function as an argument::"
msgstr "上例用 lambda 表达式返回函数。还可以把匿名函数用作传递的实参:"
#: ../../tutorial/controlflow.rst:971
msgid "Documentation Strings"
msgstr "文档字符串"
#: ../../tutorial/controlflow.rst:978
msgid ""
"Here are some conventions about the content and formatting of documentation "
"strings."
msgstr "以下是文档字符串内容和格式的约定。"
#: ../../tutorial/controlflow.rst:981
msgid ""
"The first line should always be a short, concise summary of the object's "
"purpose. For brevity, it should not explicitly state the object's name or "
"type, since these are available by other means (except if the name happens "
"to be a verb describing a function's operation). This line should begin "
"with a capital letter and end with a period."
msgstr ""
"第一行应为对象用途的简短摘要。为保持简洁,不要在这里显式说明对象名或类型,因为可通过其他方式获取这些信息(除非该名称碰巧是描述函数操作的动词)。这一行应以大写字母开头,以句点结尾。"
#: ../../tutorial/controlflow.rst:987
msgid ""
"If there are more lines in the documentation string, the second line should "
"be blank, visually separating the summary from the rest of the description."
" The following lines should be one or more paragraphs describing the "
"object's calling conventions, its side effects, etc."
msgstr "文档字符串为多行时,第二行应为空白行,在视觉上将摘要与其余描述分开。后面的行可包含若干段落,描述对象的调用约定、副作用等。"
#: ../../tutorial/controlflow.rst:992
msgid ""
"The Python parser does not strip indentation from multi-line string literals"
" in Python, so tools that process documentation have to strip indentation if"
" desired. This is done using the following convention. The first non-blank "
"line *after* the first line of the string determines the amount of "
"indentation for the entire documentation string. (We can't use the first "
"line since it is generally adjacent to the string's opening quotes so its "
"indentation is not apparent in the string literal.) Whitespace "
"\"equivalent\" to this indentation is then stripped from the start of all "
"lines of the string. Lines that are indented less should not occur, but if "
"they occur all their leading whitespace should be stripped. Equivalence of "
"whitespace should be tested after expansion of tabs (to 8 spaces, normally)."
msgstr ""
"Python 解析器不会删除 Python 中多行字符串字面值的缩进,因此,文档处理工具应在必要时删除缩进。这项操作遵循以下约定:文档字符串第一行 "
"*之后* "
"的第一个非空行决定了整个文档字符串的缩进量(第一行通常与字符串开头的引号相邻,其缩进在字符串中并不明显,因此,不能用第一行的缩进),然后,删除字符串中所有行开头处与此缩进“等价”的空白符。不能有比此缩进更少的行,但如果出现了缩进更少的行,应删除这些行的所有前导空白符。转化制表符后(通常为"
" 8 个空格),应测试空白符的等效性。"
#: ../../tutorial/controlflow.rst:1004
msgid "Here is an example of a multi-line docstring::"
msgstr "下面是多行文档字符串的一个例子:"
#: ../../tutorial/controlflow.rst:1022
msgid "Function Annotations"
msgstr "函数注解"
#: ../../tutorial/controlflow.rst:1030
msgid ""
":ref:`Function annotations <function>` are completely optional metadata "
"information about the types used by user-defined functions (see :pep:`3107` "
"and :pep:`484` for more information)."
msgstr ""
":ref:`函数注解 <function>` 是可选的用户自定义函数类型的元数据完整信息(详见 :pep:`3107` 和 :pep:`484` )。"
#: ../../tutorial/controlflow.rst:1034
msgid ""
":term:`Annotations <function annotation>` are stored in the "
":attr:`__annotations__` attribute of the function as a dictionary and have "
"no effect on any other part of the function. Parameter annotations are "
"defined by a colon after the parameter name, followed by an expression "
"evaluating to the value of the annotation. Return annotations are defined "
"by a literal ``->``, followed by an expression, between the parameter list "
"and the colon denoting the end of the :keyword:`def` statement. The "
"following example has a required argument, an optional argument, and the "
"return value annotated::"
msgstr ""
":term:`标注 <function annotation>` 以字典的形式存放在函数的 :attr:`__annotations__` "
"属性中,并且不会影响函数的任何其他部分。 形参标注的定义方式是在形参名后加冒号,后面跟一个表达式,该表达式会被求值为标注的值。 "
"返回值标注的定义方式是加组合符号 ``->``,后面跟一个表达式,该标注位于形参列表和表示 :keyword:`def` 语句结束的冒号之间。 "
"下面的示例有一个必须的参数,一个可选的关键字参数以及返回值都带有相应的标注::"
#: ../../tutorial/controlflow.rst:1056
msgid "Intermezzo: Coding Style"
msgstr "小插曲:编码风格"
#: ../../tutorial/controlflow.rst:1061
msgid ""
"Now that you are about to write longer, more complex pieces of Python, it is"
" a good time to talk about *coding style*. Most languages can be written "
"(or more concise, *formatted*) in different styles; some are more readable "
"than others. Making it easy for others to read your code is always a good "
"idea, and adopting a nice coding style helps tremendously for that."
msgstr ""
"现在你将要写更长,更复杂的 Python 代码,是时候讨论一下 *代码风格* 了。 "
"大多数语言都能以不同的风格被编写(或更准确地说,被格式化);有些比其他的更具有可读性。 "
"能让其他人轻松阅读你的代码总是一个好主意,采用一种好的编码风格对此有很大帮助。"
#: ../../tutorial/controlflow.rst:1067
msgid ""
"For Python, :pep:`8` has emerged as the style guide that most projects "