-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathfunctional.po
1692 lines (1486 loc) · 95.4 KB
/
functional.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-2018, Python Software Foundation
# This file is distributed under the same license as the Python package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: Python 3.7\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2018-08-08 08:48+0900\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: Naoki INADA <[email protected]>, 2017\n"
"Language-Team: Japanese (https://www.transifex.com/python-doc/teams/5390/ja/)\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Language: ja\n"
"Plural-Forms: nplurals=1; plural=0;\n"
#: ../../howto/functional.rst:3
msgid "Functional Programming HOWTO"
msgstr "関数型プログラミング HOWTO"
#: ../../howto/functional.rst:0
msgid "Author"
msgstr "著者"
#: ../../howto/functional.rst:5
msgid "A. M. Kuchling"
msgstr "A. M. Kuchling"
#: ../../howto/functional.rst:0
msgid "Release"
msgstr "リリース"
#: ../../howto/functional.rst:6
msgid "0.32"
msgstr "0.32"
#: ../../howto/functional.rst:8
msgid ""
"In this document, we'll take a tour of Python's features suitable for "
"implementing programs in a functional style. After an introduction to the "
"concepts of functional programming, we'll look at language features such as "
":term:`iterator`\\s and :term:`generator`\\s and relevant library modules "
"such as :mod:`itertools` and :mod:`functools`."
msgstr ""
"この文書では、関数型スタイルでプログラムを実装するのにピッタリな Python "
"の機能を見てまわることにしましょう。まず関数型プログラミングという概念を紹介したあと、 :term:`iterator` や "
":term:`generator` のような言語機能、および :mod:`itertools` や :mod:`functools` "
"といった関連するライブラリモジュールを見ることにします。"
#: ../../howto/functional.rst:16
msgid "Introduction"
msgstr "はじめに"
#: ../../howto/functional.rst:18
msgid ""
"This section explains the basic concept of functional programming; if you're"
" just interested in learning about Python language features, skip to the "
"next section on :ref:`functional-howto-iterators`."
msgstr ""
"この章は関数型プログラミングの基本概念を説明します; Python の言語機能についてだけ知りたい人は、次の章の :ref:`functional-"
"howto-iterators` まで飛ばしてください。"
#: ../../howto/functional.rst:22
msgid ""
"Programming languages support decomposing problems in several different "
"ways:"
msgstr "プログラミング言語とは問題を分解するものですが、各言語がサポートする分解方法にはいくつかの種類があります:"
#: ../../howto/functional.rst:24
msgid ""
"Most programming languages are **procedural**: programs are lists of "
"instructions that tell the computer what to do with the program's input. C,"
" Pascal, and even Unix shells are procedural languages."
msgstr ""
"ほとんどのプログラミング言語は **手続き型** です: プログラムは、入力に対して行うべきことをコンピュータに教える指示リストとなります。 C, "
"Pascal, さらには Unix シェルまでもが手続き型言語に入ります。"
#: ../../howto/functional.rst:28
msgid ""
"In **declarative** languages, you write a specification that describes the "
"problem to be solved, and the language implementation figures out how to "
"perform the computation efficiently. SQL is the declarative language you're"
" most likely to be familiar with; a SQL query describes the data set you "
"want to retrieve, and the SQL engine decides whether to scan tables or use "
"indexes, which subclauses should be performed first, etc."
msgstr ""
"**宣言型** 言語で書くのは、解くべき問題を説明する仕様書であって、それを効率的に計算処理する方法を見付けるのは言語実装の役目です。SQL "
"はおそらく一番よく知られた宣言型言語です; SQL "
"のクエリは取得したいデータセットを説明しているだけで、テーブルを走査するかインデックスを使うか、どのサブクローズから実行するか等々を決めるのは SQL "
"エンジンなのです。"
#: ../../howto/functional.rst:35
msgid ""
"**Object-oriented** programs manipulate collections of objects. Objects "
"have internal state and support methods that query or modify this internal "
"state in some way. Smalltalk and Java are object-oriented languages. C++ "
"and Python are languages that support object-oriented programming, but don't"
" force the use of object-oriented features."
msgstr ""
"**オブジェクト指向** "
"プログラムはオブジェクトの集まりを操作します。オブジェクトには内部状態があり、その状態を調べたり色々と変更したりするためのメソッドがあります。Smalltalk"
" や Java はオブジェクト指向言語です。 C++ と Python "
"はオブジェクト指向プログラミングをサポートしていますが、関連する機能を使わなくても構わないようになっています。"
#: ../../howto/functional.rst:41
msgid ""
"**Functional** programming decomposes a problem into a set of functions. "
"Ideally, functions only take inputs and produce outputs, and don't have any "
"internal state that affects the output produced for a given input. Well-"
"known functional languages include the ML family (Standard ML, OCaml, and "
"other variants) and Haskell."
msgstr ""
"**関数型** "
"プログラミングは問題をいくつかの関数にわけて考えます。理想的に言うと、関数は入力を受けて出力を吐くだけで、同じ入力に対して異なる出力をするような内部状態を一切持ちません。有名な関数型言語には"
" ML 一家 (Standard ML, OCaml 等々) と Haskell があります。"
#: ../../howto/functional.rst:47
msgid ""
"The designers of some computer languages choose to emphasize one particular "
"approach to programming. This often makes it difficult to write programs "
"that use a different approach. Other languages are multi-paradigm languages"
" that support several different approaches. Lisp, C++, and Python are multi-"
"paradigm; you can write programs or libraries that are largely procedural, "
"object-oriented, or functional in all of these languages. In a large "
"program, different sections might be written using different approaches; the"
" GUI might be object-oriented while the processing logic is procedural or "
"functional, for example."
msgstr ""
"設計者が特定のアプローチを強調することにした言語もありますが、そうすると大抵は、別のアプローチを使うプログラムを書きにくくなります。複数のアプローチに対応した言語もあり、Lisp,"
" C++, Python はそうしたマルチパラダイム言語です; "
"この中のどれを使っても、基本的に手続き型な、または基本的にオブジェクト指向な、とか、基本的に関数型なプログラムやライブラリを書くことができます。大きなプログラムでは、各部で別々のアプローチを使って書くことがあるかもしれません;"
" GUI はオブジェクト指向で、でも処理ロジックは手続き型や関数型で、といったようにです。"
#: ../../howto/functional.rst:58
msgid ""
"In a functional program, input flows through a set of functions. Each "
"function operates on its input and produces some output. Functional style "
"discourages functions with side effects that modify internal state or make "
"other changes that aren't visible in the function's return value. Functions"
" that have no side effects at all are called **purely functional**. "
"Avoiding side effects means not using data structures that get updated as a "
"program runs; every function's output must only depend on its input."
msgstr ""
"関数型プログラムでは、入力は一連の関数を通って流れていきます。それぞれの関数は入力に何らかの作業をして出力します。関数型スタイルにおいては、内部状態を変えてしまったり、返り値に現れない変更をしたりといった副作用のある関数はやめるように言われています。副作用のまったくない関数は"
" **純粋関数型** "
"であるとされます。副作用をなくすということは、プログラムの実行中に順次変化していくデータ構造を持たない、つまり各関数の出力はその入力にしか影響を受けてはいけないということです。"
#: ../../howto/functional.rst:66
msgid ""
"Some languages are very strict about purity and don't even have assignment "
"statements such as ``a=3`` or ``c = a + b``, but it's difficult to avoid all"
" side effects. Printing to the screen or writing to a disk file are side "
"effects, for example. For example, in Python a call to the :func:`print` or"
" :func:`time.sleep` function both return no useful value; they're only "
"called for their side effects of sending some text to the screen or pausing "
"execution for a second."
msgstr ""
"この純粋性を守る面で非常に厳しい言語もあり、そうした言語には ``a=3`` や ``c = a + b`` "
"といった代入文さえありません。しかし副作用を完全になくすのは難しいもので、たとえば画面表示やディスクファイルへの書き込みも副作用なのです。 Python"
" で言うと、たとえば :func:`print` や :func:`time.sleep` 関数の呼び出しはどちらも意味ある値を返しません; "
"ただ画面にテキストを送ったり動作を 1 秒止めたりといった副作用のためだけに呼ばれるのです。"
#: ../../howto/functional.rst:74
msgid ""
"Python programs written in functional style usually won't go to the extreme "
"of avoiding all I/O or all assignments; instead, they'll provide a "
"functional-appearing interface but will use non-functional features "
"internally. For example, the implementation of a function will still use "
"assignments to local variables, but won't modify global variables or have "
"other side effects."
msgstr ""
"関数型スタイルで書いた Python プログラムはふつう、I/O "
"や代入を完全になくすといった極端なところまでは行かずに、関数型っぽく見えるインタフェースを提供しつつも内部では非関数型の機能を使います。たとえば、関数内でローカル変数の代入は使いますが、グローバル変数は変更せず、他の副作用もないように実装するのです。"
#: ../../howto/functional.rst:80
msgid ""
"Functional programming can be considered the opposite of object-oriented "
"programming. Objects are little capsules containing some internal state "
"along with a collection of method calls that let you modify this state, and "
"programs consist of making the right set of state changes. Functional "
"programming wants to avoid state changes as much as possible and works with "
"data flowing between functions. In Python you might combine the two "
"approaches by writing functions that take and return instances representing "
"objects in your application (e-mail messages, transactions, etc.)."
msgstr ""
"関数型プログラミングはオブジェクト指向プログラミングの反対と考えることもできます。オブジェクト指向において、オブジェクトは内部状態とそれを変更するメソッドコールの入ったカプセルであり、プログラムはその状態を適正に変化させていく手順です。一方で、関数型プログラミングは可能なかぎり状態の変更を避け、関数どうしの間を流れるデータだけを扱おうとします。Python"
" ではこの二つのアプローチを結び合わせることができます。アプリケーション内のオブジェクト (メール、トランザクション、等々) "
"を表現したインスタンスを、関数が受け渡しするようにするのです。"
#: ../../howto/functional.rst:89
msgid ""
"Functional design may seem like an odd constraint to work under. Why should"
" you avoid objects and side effects? There are theoretical and practical "
"advantages to the functional style:"
msgstr ""
"関数型デザインは、わけのわからない制約に見えるかもしれません。どうしてオブジェクトも副作用もないほうが良いのでしょうか。実は、関数型スタイルには理論と実践に基づく次の利点があるのです:"
#: ../../howto/functional.rst:93
msgid "Formal provability."
msgstr "形式的証明可能性。"
#: ../../howto/functional.rst:94
msgid "Modularity."
msgstr "モジュラー性。"
#: ../../howto/functional.rst:95
msgid "Composability."
msgstr "結合性。"
#: ../../howto/functional.rst:96
msgid "Ease of debugging and testing."
msgstr "デバッグやテストの簡単さ。"
#: ../../howto/functional.rst:100
msgid "Formal provability"
msgstr "形式的証明可能性"
#: ../../howto/functional.rst:102
msgid ""
"A theoretical benefit is that it's easier to construct a mathematical proof "
"that a functional program is correct."
msgstr "理論面の利点としては、プログラムが正しいことの数学的証明を他より簡単に構築できるという点があります。"
#: ../../howto/functional.rst:105
msgid ""
"For a long time researchers have been interested in finding ways to "
"mathematically prove programs correct. This is different from testing a "
"program on numerous inputs and concluding that its output is usually "
"correct, or reading a program's source code and concluding that the code "
"looks right; the goal is instead a rigorous proof that a program produces "
"the right result for all possible inputs."
msgstr ""
"研究者たちは長いあいだ、プログラムが正しいことを数学的に証明する方法の発見に血道をあげてきました。これは、色々な入力でテストして出力が正しかったからまあ正しいだろう、と結論するのとも違いますし、ソースコードを読んで「間違いはなさそうだ」と言うのとも別の話です;"
" 目指すのは、出現しうる入力すべてに対してプログラムが正しい結果を出すことの厳密な証明なのです。"
#: ../../howto/functional.rst:112
msgid ""
"The technique used to prove programs correct is to write down "
"**invariants**, properties of the input data and of the program's variables "
"that are always true. For each line of code, you then show that if "
"invariants X and Y are true **before** the line is executed, the slightly "
"different invariants X' and Y' are true **after** the line is executed. "
"This continues until you reach the end of the program, at which point the "
"invariants should match the desired conditions on the program's output."
msgstr ""
"プログラムを証明するために使われているのは **不変式** "
"を書き出していくというテクニックで、不変式とは入力データやプログラム変数のうち常に真である性質のことです。コードの一行一行で、 **実行前** の不変式"
" X と Y が真なら **実行後に** ちょっと違う不変式 X' と Y' "
"が真になることを示していき、これをプログラムの終わりまで続けるわけです。すると最終的な不変式はプログラムの出力に合った条件になっているはずです。"
#: ../../howto/functional.rst:120
msgid ""
"Functional programming's avoidance of assignments arose because assignments "
"are difficult to handle with this technique; assignments can break "
"invariants that were true before the assignment without producing any new "
"invariants that can be propagated onward."
msgstr ""
"関数型プログラミングが代入を嫌うのは、この不変式テクニックでは代入を扱いにくいからです; "
"代入は、それまで真だった不変式を壊しておいて、自分は次の行に伝えてゆける不変式を生み出さないことがあるのです。"
#: ../../howto/functional.rst:125
msgid ""
"Unfortunately, proving programs correct is largely impractical and not "
"relevant to Python software. Even trivial programs require proofs that are "
"several pages long; the proof of correctness for a moderately complicated "
"program would be enormous, and few or none of the programs you use daily "
"(the Python interpreter, your XML parser, your web browser) could be proven "
"correct. Even if you wrote down or generated a proof, there would then be "
"the question of verifying the proof; maybe there's an error in it, and you "
"wrongly believe you've proved the program correct."
msgstr ""
"残念ながら、プログラムの証明はだいたい実際的でもありませんし、Python "
"ソフトウェアにも関係ありません。本当に簡単なプログラムでも、証明には数ページにわたる論文が必要なのです; "
"ある程度の複雑なプログラムではもう尋常でない長さになってしまうので、日常で使っているプログラム (Python インタプリタ、XML "
"パーサ、ウェブブラウザ) "
"はほとんど、あるいはすべて、正しさを証明するのは不可能でしょう。仮に証明を書き出したり生成したりしても、その証明を検証するための疑いが残ります; "
"証明に間違いがあるかもしれず、その場合は証明したと自分で勝手に思い込んでいただけになるのです。"
#: ../../howto/functional.rst:136
msgid "Modularity"
msgstr "モジュラー性"
#: ../../howto/functional.rst:138
msgid ""
"A more practical benefit of functional programming is that it forces you to "
"break apart your problem into small pieces. Programs are more modular as a "
"result. It's easier to specify and write a small function that does one "
"thing than a large function that performs a complicated transformation. "
"Small functions are also easier to read and to check for errors."
msgstr ""
"より実用的には、関数型プログラミングをすると問題を細かく切り分けることになるという利点があります。結果としてプログラムはモジュラー化されます。複雑な変形を施す大きな関数を書くより、一つのことに絞ってそれだけをする小さな関数のほうが書きやすいものです。それに、小さいほうが読むのもエラーをチェックするのも簡単です。"
#: ../../howto/functional.rst:146
msgid "Ease of debugging and testing"
msgstr "デバグやテストの簡単さ"
#: ../../howto/functional.rst:148
msgid "Testing and debugging a functional-style program is easier."
msgstr "テストやデバグも関数型プログラムなら簡単です。"
#: ../../howto/functional.rst:150
msgid ""
"Debugging is simplified because functions are generally small and clearly "
"specified. When a program doesn't work, each function is an interface point"
" where you can check that the data are correct. You can look at the "
"intermediate inputs and outputs to quickly isolate the function that's "
"responsible for a bug."
msgstr ""
"関数が一般的に小さくて明確に意味付けされているので、デバグ方法は単純です。プログラムが正しく動かないときには、関数ひとつひとつがデータの正しさをチェックするポイントになるので、それぞれの時点における入力と出力を見ていけば、バグの原因となる関数を素早く切り出すことができるのです。"
#: ../../howto/functional.rst:155
msgid ""
"Testing is easier because each function is a potential subject for a unit "
"test. Functions don't depend on system state that needs to be replicated "
"before running a test; instead you only have to synthesize the right input "
"and then check that the output matches expectations."
msgstr ""
"ひとつひとつの関数がユニットテストの対象になり得るわけですから、テストも簡単です。関数はシステムの状態に依存しませんので、テストの実行前にそうした状態を再現する必要はありません;"
" 単に適切な入力を合成して、出力が期待どおりかどうかチェックするだけで良いのです。"
#: ../../howto/functional.rst:162
msgid "Composability"
msgstr "結合性"
#: ../../howto/functional.rst:164
msgid ""
"As you work on a functional-style program, you'll write a number of "
"functions with varying inputs and outputs. Some of these functions will be "
"unavoidably specialized to a particular application, but others will be "
"useful in a wide variety of programs. For example, a function that takes a "
"directory path and returns all the XML files in the directory, or a function"
" that takes a filename and returns its contents, can be applied to many "
"different situations."
msgstr ""
"関数型スタイルのプログラムを作っていると、色々な入力や出力のために色々な関数を書くことになります。仕方なく特定のアプリケーションに特化した関数を書くこともあるでしょうけれど、広範なプログラムに使える関数もあることでしょう。たとえば、ディレクトリ名を受け取ってその中の"
" XML ファイル一覧を返す関数や、ファイル名を受け取って内容を返す関数などは、多様な場面に適用できそうです。"
#: ../../howto/functional.rst:171
msgid ""
"Over time you'll form a personal library of utilities. Often you'll "
"assemble new programs by arranging existing functions in a new configuration"
" and writing a few functions specialized for the current task."
msgstr ""
"時たつうちに自分の特製ライブラリやユーティリティが充実してくると、新しいプログラムも、既存の関数を調整して少し今回に特化した関数を書くだけで組み立てられるようになります。"
#: ../../howto/functional.rst:179
msgid "Iterators"
msgstr "イテレータ (iterator)"
#: ../../howto/functional.rst:181
msgid ""
"I'll start by looking at a Python language feature that's an important "
"foundation for writing functional-style programs: iterators."
msgstr "まずは関数型スタイルのプログラムを書く際の基礎となる重要な Python 機能から見ていきましょう: イテレータです。"
#: ../../howto/functional.rst:184
msgid ""
"An iterator is an object representing a stream of data; this object returns "
"the data one element at a time. A Python iterator must support a method "
"called :meth:`~iterator.__next__` that takes no arguments and always returns"
" the next element of the stream. If there are no more elements in the "
"stream, :meth:`~iterator.__next__` must raise the :exc:`StopIteration` "
"exception. Iterators don't have to be finite, though; it's perfectly "
"reasonable to write an iterator that produces an infinite stream of data."
msgstr ""
"イテレータは連続データを表現するオブジェクトです; このオブジェクトは一度に一つの要素ずつデータを返します。 Python のイテレータは "
":meth:`~iterator.__next__` "
"という、引数を取らず次の要素を返すメソッドを必ずサポートしています。データストリームに要素が残っていない場合、 "
":meth:`~iterator.__next__` は必ず :exc:`StopIteration` "
"例外を出します。ただし、イテレータの長さは有限である必要はありません; 無限のストリームを生成するイテレータを書くというのもまったく理に適ったことです。"
#: ../../howto/functional.rst:192
msgid ""
"The built-in :func:`iter` function takes an arbitrary object and tries to "
"return an iterator that will return the object's contents or elements, "
"raising :exc:`TypeError` if the object doesn't support iteration. Several "
"of Python's built-in data types support iteration, the most common being "
"lists and dictionaries. An object is called :term:`iterable` if you can get"
" an iterator for it."
msgstr ""
"ビルトインの :func:`iter` 関数は任意のオブジェクトを受けて、 その中身や要素を返すイテレータを返そうとします。引数のオブジェクトが "
"イテレータを作れないときは :exc:`TypeError` を投げます。Python の "
"ビルトインなデータ型にもいくつかイテレータ化のできるものがあり、 中でもよく使われるのはリストと辞書です。イテレータを作れる オブジェクトは "
":term:`iterable` オブジェクトと呼ばれます。"
#: ../../howto/functional.rst:199
msgid "You can experiment with the iteration interface manually:"
msgstr "手を動かしてイテレータ化の実験をしてみましょう:"
#: ../../howto/functional.rst:217
msgid ""
"Python expects iterable objects in several different contexts, the most "
"important being the :keyword:`for` statement. In the statement ``for X in "
"Y``, Y must be an iterator or some object for which :func:`iter` can create "
"an iterator. These two statements are equivalent::"
msgstr ""
"Python は色々な文脈でイテラブルなオブジェクトを期待しますが、 最も重要なのは :keyword:`for` 文です。 ``for X in "
"Y`` という文の Y は、 イテレータか、あるいは :func:`iter` "
"でイテレータを作れるオブジェクトである必要があります。次の二つは同じ意味になります::"
#: ../../howto/functional.rst:229
msgid ""
"Iterators can be materialized as lists or tuples by using the :func:`list` "
"or :func:`tuple` constructor functions:"
msgstr ""
"イテレータは :func:`list` や :func:`tuple` といったコンストラクタ関数を使ってリストやタプルに具現化することができます:"
#: ../../howto/functional.rst:238
msgid ""
"Sequence unpacking also supports iterators: if you know an iterator will "
"return N elements, you can unpack them into an N-tuple:"
msgstr ""
"シーケンスのアンパックもイテレータに対応しています: イテレータが N "
"個の要素を返すということが事前にわかっていれば、N-タプルにアンパックすることができます:"
#: ../../howto/functional.rst:247
msgid ""
"Built-in functions such as :func:`max` and :func:`min` can take a single "
"iterator argument and will return the largest or smallest element. The "
"``\"in\"`` and ``\"not in\"`` operators also support iterators: ``X in "
"iterator`` is true if X is found in the stream returned by the iterator. "
"You'll run into obvious problems if the iterator is infinite; :func:`max`, "
":func:`min` will never return, and if the element X never appears in the "
"stream, the ``\"in\"`` and ``\"not in\"`` operators won't return either."
msgstr ""
"ビルトイン関数の :func:`max` や :func:`min` なども、イテレータ一つだけを引数に取って最大・最小の要素を返すことができます。 "
"``\"in\"`` や ``\"not in\"`` 演算子もイテレータに対応しています: ``X in iterator`` "
"は、そのイテレータから返るストリームに X があれば真です。ですからイテレータが無限長だと、当然ながら問題に直面します; :func:`max`, "
":func:`min` はいつまでも戻って来ませんし、 要素 X がストリームに出てこなければ ``\"in\"``, ``\"not in\"`` "
"オペレータも戻りません。"
#: ../../howto/functional.rst:255
msgid ""
"Note that you can only go forward in an iterator; there's no way to get the "
"previous element, reset the iterator, or make a copy of it. Iterator "
"objects can optionally provide these additional capabilities, but the "
"iterator protocol only specifies the :meth:`~iterator.__next__` method. "
"Functions may therefore consume all of the iterator's output, and if you "
"need to do something different with the same stream, you'll have to create a"
" new iterator."
msgstr ""
"イテレータは次に進むことしかできませんのでご注意ください; "
"前の要素を手に入れたり、イテレータをリセットしたり、コピーを作ったりする方法はありません。イテレータがオブジェクトとしてそうした追加機能を "
"持つことはできますが、プロトコルでは :meth:`~iterator.__next__` "
"メソッドのことしか指定されていません。ですから関数はイテレータの出力を使い尽くして しまうかもしれませんし、同じストリームに何か別のことをする "
"必要があるなら新しいイテレータを作らなくてはいけません。"
#: ../../howto/functional.rst:265
msgid "Data Types That Support Iterators"
msgstr "イテレータ対応のデータ型"
#: ../../howto/functional.rst:267
msgid ""
"We've already seen how lists and tuples support iterators. In fact, any "
"Python sequence type, such as strings, will automatically support creation "
"of an iterator."
msgstr ""
"リストやタプルがイテレータに対応している方法については既に見ましたが、実のところ Python "
"のシーケンス型はどれでも、たとえば文字列なども、自動でイテレータ生成に対応しています。"
#: ../../howto/functional.rst:271
msgid ""
"Calling :func:`iter` on a dictionary returns an iterator that will loop over"
" the dictionary's keys::"
msgstr "辞書に対して :func:`iter` すると、辞書のキーでループを回すイテレータが返されます::"
#: ../../howto/functional.rst:291
msgid ""
"Note that starting with Python 3.7, dictionary iteration order is guaranteed"
" to be the same as the insertion order. In earlier versions, the behaviour "
"was unspecified and could vary between implementations."
msgstr ""
"Python 3.7 から、辞書の反復順序は挿入順序と同じであることが保証されていることに注意してください。\n"
"以前のバージョンでは、その振る舞いは仕様が定められておらず、実装ごとに異なることがありました。"
#: ../../howto/functional.rst:295
msgid ""
"Applying :func:`iter` to a dictionary always loops over the keys, but "
"dictionaries have methods that return other iterators. If you want to "
"iterate over values or key/value pairs, you can explicitly call the "
":meth:`~dict.values` or :meth:`~dict.items` methods to get an appropriate "
"iterator."
msgstr ""
"辞書は :func:`iter` "
"を適用するとキーでループを回しますが、辞書には他のイテレータを返すメソッドもあります。明示的に値、あるいはキーと値のペアでイテレートしたければ、:meth:`~dict.values`,"
" :meth:`~dict.items` というメソッドでイテレータを作ることができます。"
#: ../../howto/functional.rst:301
msgid ""
"The :func:`dict` constructor can accept an iterator that returns a finite "
"stream of ``(key, value)`` tuples:"
msgstr ""
"逆に :func:`dict` コンストラクタは、有限な ``(key, value)`` "
"タプルのストリームを返すイテレータを受け入れることができます:"
#: ../../howto/functional.rst:308
msgid ""
"Files also support iteration by calling the :meth:`~io.TextIOBase.readline` "
"method until there are no more lines in the file. This means you can read "
"each line of a file like this::"
msgstr ""
"ファイルも、最後の行まで :meth:`~io.TextIOBase.readline` "
"メソッドを呼んでいくことでイテレータ化に対応しています。つまりこうやってファイルの各行を読んでいくことができるわけです::"
#: ../../howto/functional.rst:316
msgid ""
"Sets can take their contents from an iterable and let you iterate over the "
"set's elements::"
msgstr "セットはイテラブルを受け取れますし、そのセットの要素でイテレートすることもできます::"
#: ../../howto/functional.rst:326
msgid "Generator expressions and list comprehensions"
msgstr "ジェネレータ式とリスト内包表記"
#: ../../howto/functional.rst:328
msgid ""
"Two common operations on an iterator's output are 1) performing some "
"operation for every element, 2) selecting a subset of elements that meet "
"some condition. For example, given a list of strings, you might want to "
"strip off trailing whitespace from each line or extract all the strings "
"containing a given substring."
msgstr ""
"イテレータの出力に対してよく使う操作トップ 2 は、(1) ひとつずつ全要素に操作を実行する、および (2) "
"条件に合う要素でサブセットを作る、です。たとえば文字列のリストなら、各行のうしろに付いた邪魔なホワイトスペースを削りたいとか、特定の文字列を含む部分をピックアップしたいなどと思うかもしれません。"
#: ../../howto/functional.rst:334
msgid ""
"List comprehensions and generator expressions (short form: \"listcomps\" and"
" \"genexps\") are a concise notation for such operations, borrowed from the "
"functional programming language Haskell (https://www.haskell.org/). You can"
" strip all the whitespace from a stream of strings with the following code::"
msgstr ""
"リスト内包表記とジェネレータ式 (略して「listcomp」と「genexp」) "
"は、そうした操作向けの簡潔な表記方法です。これは関数型プログラミング言語 Haskell (https://www.haskell.org/) "
"にインスパイアされました。文字列のストリームからホワイトスペースをすべて削るのは次のコードでできます::"
#: ../../howto/functional.rst:347
msgid "You can select only certain elements by adding an ``\"if\"`` condition::"
msgstr "特定の要素だけを選び出すのは ``\"if\"`` 条件式を付けることで可能です::"
#: ../../howto/functional.rst:352
msgid ""
"With a list comprehension, you get back a Python list; ``stripped_list`` is "
"a list containing the resulting lines, not an iterator. Generator "
"expressions return an iterator that computes the values as necessary, not "
"needing to materialize all the values at once. This means that list "
"comprehensions aren't useful if you're working with iterators that return an"
" infinite stream or a very large amount of data. Generator expressions are "
"preferable in these situations."
msgstr ""
"リスト内包表記を使うと Python リストが返って来ます; ``stripped_list`` "
"は実行結果の行が入ったリストであって、イテレータではありません。ジェネレータ式はイテレータを返し、これだと必要に応じてだけ値を算出しますので、すべての値を一度に出す必要がありません。つまりリスト内包表記のほうは、無限長ストリームや膨大なデータを返すようなイテレータを扱う際には、あまり役に立たないということです。そういった状況ではジェネレータ式のほうが好ましいと言えます。"
#: ../../howto/functional.rst:359
msgid ""
"Generator expressions are surrounded by parentheses (\"()\") and list "
"comprehensions are surrounded by square brackets (\"[]\"). Generator "
"expressions have the form::"
msgstr "ジェネレータ式は丸括弧 \"()\" で囲まれ、リスト内包表記は角括弧 \"[]\" で囲まれます。ジェネレータ式の形式は次のとおりです::"
#: ../../howto/functional.rst:372
msgid ""
"Again, for a list comprehension only the outside brackets are different "
"(square brackets instead of parentheses)."
msgstr "リスト内包表記も、外側の括弧が違うだけ (丸ではなく角括弧) で、あとは同じです。"
#: ../../howto/functional.rst:375
msgid ""
"The elements of the generated output will be the successive values of "
"``expression``. The ``if`` clauses are all optional; if present, "
"``expression`` is only evaluated and added to the result when ``condition`` "
"is true."
msgstr ""
"生成される出力は ``expression`` 部分の値を要素として並べたものになります。 ``if`` 節はすべて、なくても大丈夫です; あれば "
"``condition`` が真のときだけ ``expression`` が評価されて出力に追加されます。"
#: ../../howto/functional.rst:379
msgid ""
"Generator expressions always have to be written inside parentheses, but the "
"parentheses signalling a function call also count. If you want to create an"
" iterator that will be immediately passed to a function you can write::"
msgstr ""
"ジェネレータ式は常に括弧の中に書かなければなりませんが、関数コールの目印になっている括弧でも大丈夫です。関数にすぐ渡すイテレータを作りたければこう書けるのです::"
#: ../../howto/functional.rst:385
msgid ""
"The ``for...in`` clauses contain the sequences to be iterated over. The "
"sequences do not have to be the same length, because they are iterated over "
"from left to right, **not** in parallel. For each element in ``sequence1``,"
" ``sequence2`` is looped over from the beginning. ``sequence3`` is then "
"looped over for each resulting pair of elements from ``sequence1`` and "
"``sequence2``."
msgstr ""
"``for...in`` 節は複数つなげられますが、どれにも、イテレートするためのシーケンスが含まれています。それらのシーケンスは並行して "
"**ではなく** 、左から右へ順番にイテレートされるので、長さが同じである必要はありません。 ``sequence1`` の各要素ごとに毎回最初から "
"``sequence2`` をループで回すのです。その後 ``sequence1`` と ``sequence2`` から出た要素ペアごとに、 "
"``sequence3`` でループします。"
#: ../../howto/functional.rst:391
msgid ""
"To put it another way, a list comprehension or generator expression is "
"equivalent to the following Python code::"
msgstr "別の書き方をすると、リスト内包表記やジェネレータ式は次の Python コードと同じ意味になります::"
#: ../../howto/functional.rst:408
msgid ""
"This means that when there are multiple ``for...in`` clauses but no ``if`` "
"clauses, the length of the resulting output will be equal to the product of "
"the lengths of all the sequences. If you have two lists of length 3, the "
"output list is 9 elements long:"
msgstr ""
"つまり、複数の ``for...in`` 節があって ``if`` がないときの最終出力は、長さが各シーケンス長の積に等しくなるということです。長さ 3"
" のリスト二つなら、出力リストの長さは 9 要素です:"
#: ../../howto/functional.rst:420
msgid ""
"To avoid introducing an ambiguity into Python's grammar, if ``expression`` "
"is creating a tuple, it must be surrounded with parentheses. The first list"
" comprehension below is a syntax error, while the second one is correct::"
msgstr ""
"Python の文法に曖昧さを紛れ込ませないように、 ``expression`` "
"でタプルを作るなら括弧で囲わなくてはなりません。下にあるリスト内包表記で、最初のは構文エラーですが、二番目は有効です::"
#: ../../howto/functional.rst:431
msgid "Generators"
msgstr "ジェネレータ (generator)"
#: ../../howto/functional.rst:433
msgid ""
"Generators are a special class of functions that simplify the task of "
"writing iterators. Regular functions compute a value and return it, but "
"generators return an iterator that returns a stream of values."
msgstr ""
"ジェネレータは、イテレータを書く作業を簡単にする、特殊な関数です。標準的な関数は値を計算して返しますが、ジェネレータが返すのは、一連の値を返すイテレータです。"
#: ../../howto/functional.rst:437
msgid ""
"You're doubtless familiar with how regular function calls work in Python or "
"C. When you call a function, it gets a private namespace where its local "
"variables are created. When the function reaches a ``return`` statement, "
"the local variables are destroyed and the value is returned to the caller. "
"A later call to the same function creates a new private namespace and a "
"fresh set of local variables. But, what if the local variables weren't "
"thrown away on exiting a function? What if you could later resume the "
"function where it left off? This is what generators provide; they can be "
"thought of as resumable functions."
msgstr ""
"Python や C "
"の標準的な関数コールについては、よくご存じに違いありません。関数を呼ぶと、ローカル変数を作るプライベートな名前空間ができますね。その関数が "
"``return`` "
"文まで来ると、ローカル変数が破壊されてから、返り値が呼び出し元に返ります。次に同じ関数をもう一度呼ぶと、新しいプライベート名前空間に新規のローカル変数が作られるのです。しかし、関数を出るときにローカル変数を捨てなければどうなるでしょうか。その出ていったところから関数を続行できたとしたら、どうでしょう。これこそジェネレータが提供する機能です;"
" すなわち、ジェネレータは続行できる関数と考えることができます。"
#: ../../howto/functional.rst:446
msgid "Here's the simplest example of a generator function:"
msgstr "ごく単純なジェネレータ関数の例がこちらにあります:"
#: ../../howto/functional.rst:452
msgid ""
"Any function containing a :keyword:`yield` keyword is a generator function; "
"this is detected by Python's :term:`bytecode` compiler which compiles the "
"function specially as a result."
msgstr ""
":keyword:`yield` キーワードを含む関数はすべてジェネレータ関数です; Python の :term:`bytecode` "
"コンパイラがこれを検出して、特別な方法でコンパイルしてくれるのです。"
#: ../../howto/functional.rst:456
msgid ""
"When you call a generator function, it doesn't return a single value; "
"instead it returns a generator object that supports the iterator protocol. "
"On executing the ``yield`` expression, the generator outputs the value of "
"``i``, similar to a ``return`` statement. The big difference between "
"``yield`` and a ``return`` statement is that on reaching a ``yield`` the "
"generator's state of execution is suspended and local variables are "
"preserved. On the next call to the generator's :meth:`~generator.__next__` "
"method, the function will resume executing."
msgstr ""
"ジェネレータ関数は、呼ばれたときに一回だけ値を返すのではなく、イテレータ プロトコルに対応したオブジェクトを返します。上の例で ``yield`` "
"を実行したとき、 ジェネレータは ``return`` 文のようにして ``i`` の値を出力します。 ``yield`` と ``return`` "
"文の大きな違いは、 ``yield`` に到達した段階でジェネレータの実行状態が一時停止になって、ローカル変数が保存される点です。 "
"次回そのジェネレータの :meth:`~generator.__next__` を呼ぶと、そこから関数が実行を再開します。"
#: ../../howto/functional.rst:465
msgid "Here's a sample usage of the ``generate_ints()`` generator:"
msgstr "上記 ``generate_ints()`` ジェネレータの使用例はこちらです:"
#: ../../howto/functional.rst:482
msgid ""
"You could equally write ``for i in generate_ints(5)``, or ``a, b, c = "
"generate_ints(3)``."
msgstr ""
"同じく ``for i in generate_ints(5)`` や ``a, b, c = generate_ints(3)`` "
"といった書き方もできます。"
#: ../../howto/functional.rst:485
msgid ""
"Inside a generator function, ``return value`` causes "
"``StopIteration(value)`` to be raised from the :meth:`~generator.__next__` "
"method. Once this happens, or the bottom of the function is reached, the "
"procession of values ends and the generator cannot yield any further values."
msgstr ""
"ジェネレータ関数の中では、``return value`` は :meth:`~generator.__next__` メソッドから送出された "
"``StopIteration(value)`` "
"を引き起こします。これが発生した場合や、関数の終わりに到達した場合は、値の生成が終了してジェネレーターがそれ以上の値を返さない。"
#: ../../howto/functional.rst:490
msgid ""
"You could achieve the effect of generators manually by writing your own "
"class and storing all the local variables of the generator as instance "
"variables. For example, returning a list of integers could be done by "
"setting ``self.count`` to 0, and having the :meth:`~iterator.__next__` "
"method increment ``self.count`` and return it. However, for a moderately "
"complicated generator, writing a corresponding class can be much messier."
msgstr ""
"自分でクラスを書いて、ジェネレータで言うところのローカル変数をインスタンス変数として全部保管しておけば、同じ効果を得ることは可能です。たとえば整数のリストを返すのは、"
" ``self.count`` を 0 にして、 :meth:`~iterator.__next__` メソッドが ``self.count`` "
"をインクリメントして返すようにすればできます。しかしながら、ある程度複雑なジェネレータになってくると、同じことをするクラスを書くのは格段にややこしいことになります。"
#: ../../howto/functional.rst:498
msgid ""
"The test suite included with Python's library, "
":source:`Lib/test/test_generators.py`, contains a number of more interesting"
" examples. Here's one generator that implements an in-order traversal of a "
"tree using generators recursively. ::"
msgstr ""
"Python のライブラリに含まれているテストスイート :source:`Lib/test/test_generators.py` "
"には、ほかにも興味深い例が数多く入っています。これは二分木の通りがけ順 (in-order) 探索を再帰で実装したジェネレータです。 ::"
#: ../../howto/functional.rst:514
msgid ""
"Two other examples in ``test_generators.py`` produce solutions for the "
"N-Queens problem (placing N queens on an NxN chess board so that no queen "
"threatens another) and the Knight's Tour (finding a route that takes a "
"knight to every square of an NxN chessboard without visiting any square "
"twice)."
msgstr ""
"ほかにも ``test_generators.py`` には、N-Queens 問題 (N×N コマのチェス盤に、互いに攻撃できないような配置で N "
"個のクイーンを置く) やナイト・ツアー (N×N 盤の全コマをナイトが一度ずつ通るような経路を探す) の解を出す例が入っています。"
#: ../../howto/functional.rst:522
msgid "Passing values into a generator"
msgstr "ジェネレータに値を渡す"
#: ../../howto/functional.rst:524
msgid ""
"In Python 2.4 and earlier, generators only produced output. Once a "
"generator's code was invoked to create an iterator, there was no way to pass"
" any new information into the function when its execution is resumed. You "
"could hack together this ability by making the generator look at a global "
"variable or by passing in some mutable object that callers then modify, but "
"these approaches are messy."
msgstr ""
"Python 2.4 "
"までのジェネレータは出力することしかできませんでした。ジェネレータのコードを実行してイテレータを作ってしまったあとで、その関数を再開するときに新しい情報を渡す手段はなかったのです。ジェネレータがグローバル変数を見るようにしたり、ミュータブルなオブジェクトを渡しておいて呼び出し元であとからそれを変更したり、といったハックは可能でしたが、どれもゴチャゴチャしていますね。"
#: ../../howto/functional.rst:531
msgid ""
"In Python 2.5 there's a simple way to pass values into a generator. "
":keyword:`yield` became an expression, returning a value that can be "
"assigned to a variable or otherwise operated on::"
msgstr ""
"Python 2.5 で、ジェネレータに値を渡す簡単な手段ができました。 :keyword:`yield` "
"が、変数に代入したり演算したりできる値を返す式になったのです::"
#: ../../howto/functional.rst:537
msgid ""
"I recommend that you **always** put parentheses around a ``yield`` "
"expression when you're doing something with the returned value, as in the "
"above example. The parentheses aren't always necessary, but it's easier to "
"always add them instead of having to remember when they're needed."
msgstr ""
"上のように、返り値で何かをするときは ``yield`` 式の前後に **必ず** "
"括弧を付けるようお勧めします。括弧は常に必要なわけではありませんが、どんなとき付けなくて良いのかを覚えておくより、いつも付けておくほうが楽ですから。"
#: ../../howto/functional.rst:542
msgid ""
"(:pep:`342` explains the exact rules, which are that a ``yield``-expression "
"must always be parenthesized except when it occurs at the top-level "
"expression on the right-hand side of an assignment. This means you can "
"write ``val = yield i`` but have to use parentheses when there's an "
"operation, as in ``val = (yield i) + 12``.)"
msgstr ""
"(:pep:`342` がその規則を正確に説明していますが、それによると "
"``yield``-式は、代入式で右辺のトップレベルにあるとき以外はいつも括弧を付ける必要があります。つまり ``val = yield i`` "
"とは書けますが、 ``val = (yield i) + 12`` のように演算子があるときは括弧を使わなくてはいけません。)"
#: ../../howto/functional.rst:548
msgid ""
"Values are sent into a generator by calling its :meth:`send(value) "
"<generator.send>` method. This method resumes the generator's code and the "
"``yield`` expression returns the specified value. If the regular "
":meth:`~generator.__next__` method is called, the ``yield`` returns "
"``None``."
msgstr ""
"ジェネレータに値を送るには :meth:`send(value) <generator.send>` メソッドを呼びます。 "
"するとジェネレータのコードが実行を再開し、 ``yield`` 式がその値を返すのです。ふつうの :meth:`~generator.__next__`"
" メソッドを呼ぶと、 ``yield`` は ``None`` を返します。"
#: ../../howto/functional.rst:553
msgid ""
"Here's a simple counter that increments by 1 and allows changing the value "
"of the internal counter."
msgstr "下にあるのは 1 ずつ増える単純なカウンタですが、内部カウンタの値を変更することができるようになっています。"
#: ../../howto/functional.rst:568
msgid "And here's an example of changing the counter:"
msgstr "そしてカウンタ変更の例がこちらです:"
#: ../../howto/functional.rst:585
msgid ""
"Because ``yield`` will often be returning ``None``, you should always check "
"for this case. Don't just use its value in expressions unless you're sure "
"that the :meth:`~generator.send` method will be the only method used to "
"resume your generator function."
msgstr ""
"``yield`` が ``None`` "
"を返すことはよくあるのですから、そうなっていないかどうか必ずチェックしておくべきです。ジェネレータ関数を再開するために使うメソッドが "
":meth:`~generator.send` しかないのだと確定してるのでない限り、式の値をそのまま使ってはいけません。"
#: ../../howto/functional.rst:590
msgid ""
"In addition to :meth:`~generator.send`, there are two other methods on "
"generators:"
msgstr "ジェネレータには、 :meth:`~generator.send` のほかにもメソッドが二つあります:"
#: ../../howto/functional.rst:593
msgid ""
":meth:`throw(type, value=None, traceback=None) <generator.throw>` is used to"
" raise an exception inside the generator; the exception is raised by the "
"``yield`` expression where the generator's execution is paused."
msgstr ""
":meth:`throw(type, value=None, traceback=None) <generator.throw>` "
"はジェネレータ内で例外を投げるために使います; その例外はジェネレータの実行が停止したところの ``yield`` 式によって投げられます。"
#: ../../howto/functional.rst:597
msgid ""
":meth:`~generator.close` raises a :exc:`GeneratorExit` exception inside the "
"generator to terminate the iteration. On receiving this exception, the "
"generator's code must either raise :exc:`GeneratorExit` or "
":exc:`StopIteration`; catching the exception and doing anything else is "
"illegal and will trigger a :exc:`RuntimeError`. :meth:`~generator.close` "
"will also be called by Python's garbage collector when the generator is "
"garbage-collected."
msgstr ""
":meth:`~generator.close` はジェネレータ内で :exc:`GeneratorExit` 例外を投げて、 "
"イテレートを終了させます。この例外を受け取ったジェネレータのコードは :exc:`GeneratorExit` か "
":exc:`StopIteration` を投げなくてはいけません; この例外を捕捉して何かほかのことをしようとするのは規則違反であり、 "
":exc:`RuntimeError` を引き起こします。 :meth:`~generator.close` はジェネレータが GC "
"されるときにも呼ばれます。"
#: ../../howto/functional.rst:605
msgid ""
"If you need to run cleanup code when a :exc:`GeneratorExit` occurs, I "
"suggest using a ``try: ... finally:`` suite instead of catching "
":exc:`GeneratorExit`."
msgstr ""
":exc:`GeneratorExit` が起こったときにクリーンアップ作業をする必要があるなら、 :exc:`GeneratorExit` "
"を捕捉するのではなく ``try: ... finaly:`` するようお勧めします。"
#: ../../howto/functional.rst:608
msgid ""
"The cumulative effect of these changes is to turn generators from one-way "
"producers of information into both producers and consumers."
msgstr "これらの変更の合わせ技で、ジェネレータは情報の一方的な生産者から、生産者かつ消費者という存在に変貌を遂げたのです。"
#: ../../howto/functional.rst:611
msgid ""
"Generators also become **coroutines**, a more generalized form of "
"subroutines. Subroutines are entered at one point and exited at another "
"point (the top of the function, and a ``return`` statement), but coroutines "
"can be entered, exited, and resumed at many different points (the ``yield`` "
"statements)."
msgstr ""
"ジェネレータは **コルーチン** という、より一般化された形式のサブルーチンにもなります。サブルーチンは一カ所 (関数の冒頭) から入って別の一カ所 "
"(``return`` 文) から出るだけですが、コルーチンはいろいろな場所 (``yield`` 文) から入ったり出たり再開したりできるのです。"
#: ../../howto/functional.rst:618
msgid "Built-in functions"
msgstr "組み込み関数 (built-in function)"
#: ../../howto/functional.rst:620
msgid ""
"Let's look in more detail at built-in functions often used with iterators."
msgstr "よくイテレータと一緒に使うビルトイン関数について、もっと詳しく見ていきましょう。"
#: ../../howto/functional.rst:622
msgid ""
"Two of Python's built-in functions, :func:`map` and :func:`filter` duplicate"
" the features of generator expressions:"
msgstr "Python のビルトイン関数 :func:`map` と :func:`filter` は、機能がジェネレータ式と重複しています:"
#: ../../howto/functional.rst:634
msgid ""
":func:`map(f, iterA, iterB, ...) <map>` returns an iterator over the "
"sequence"
msgstr ":func:`map(f, iterA, iterB, ...) <map>` は以下のシーケンスのイテレータを返します"
#: ../../howto/functional.rst:626
msgid ""
"``f(iterA[0], iterB[0]), f(iterA[1], iterB[1]), f(iterA[2], iterB[2]), "
"...``."
msgstr ""
"``f(iterA[0], iterB[0]), f(iterA[1], iterB[1]), f(iterA[2], iterB[2]), "
"...``."
#: ../../howto/functional.rst:636
msgid "You can of course achieve the same effect with a list comprehension."
msgstr "もちろん、リスト内包表記でも同じ結果が得られます。"
#: ../../howto/functional.rst:638
msgid ""
":func:`filter(predicate, iter) <filter>` returns an iterator over all the "
"sequence elements that meet a certain condition, and is similarly duplicated"
" by list comprehensions. A **predicate** is a function that returns the "
"truth value of some condition; for use with :func:`filter`, the predicate "
"must take a single value."
msgstr ""
":func:`filter(predicate, iter) <filter>` はある条件を満たす要素に渡るイテレータを返すので、同様にリスト内包表記で再現できます。\n"
"**predicate** は、ある条件に対する真偽値を返す関数です;\n"
":func:`filter` で使うには、その関数の引数は一つでなければなりません。"
#: ../../howto/functional.rst:651
msgid "This can also be written as a list comprehension:"
msgstr "これはリスト内包表記でも書けます:"
#: ../../howto/functional.rst:657
msgid ""
":func:`enumerate(iter, start=0) <enumerate>` counts off the elements in the "
"iterable returning 2-tuples containing the count (from *start*) and each "
"element. ::"
msgstr ""
":func:`enumerate(iter, start=0) <enumerate>` はイテラブルの要素に順番に番号を振っていき、(*start* "
"から数え始めたときの) 番号とそれぞれの要素を含む 2 タプルを返します。"
#: ../../howto/functional.rst:667
msgid ""
":func:`enumerate` is often used when looping through a list and recording "
"the indexes at which certain conditions are met::"
msgstr ":func:`enumerate` はよく、リストに対してループさせて、条件に合う所に印を付けていくときに使われます::"
#: ../../howto/functional.rst:675
msgid ""
":func:`sorted(iterable, key=None, reverse=False) <sorted>` collects all the "
"elements of the iterable into a list, sorts the list, and returns the sorted"
" result. The *key* and *reverse* arguments are passed through to the "
"constructed list's :meth:`~list.sort` method. ::"
msgstr ""
":func:`sorted(iterable, key=None, reverse=False) <sorted>` "
"はイテラブルの要素をすべて集めたリストを作り、ソートして返します。 引数 *key* および *reverse* は、リストの "
":meth:`~list.sort` メソッドにそのまま渡されます。 ::"
#: ../../howto/functional.rst:690
msgid ""
"(For a more detailed discussion of sorting, see the :ref:`sortinghowto`.)"
msgstr "(ソートに関する詳細な論議は :ref:`sortinghowto` を参照)"
#: ../../howto/functional.rst:693
msgid ""
"The :func:`any(iter) <any>` and :func:`all(iter) <all>` built-ins look at "
"the truth values of an iterable's contents. :func:`any` returns ``True`` if"
" any element in the iterable is a true value, and :func:`all` returns "
"``True`` if all of the elements are true values:"
msgstr ""
"組み込みの :func:`any(iter) <any>` および :func:`all(iter) <all>` は iterable "
"の値の真偽を調べます。 :func:`any` は要素のどれかが真値なら ``True`` を返し、 :func:`all` は要素が全て真値なら "
"``True`` を返します:"
#: ../../howto/functional.rst:712
msgid ""
":func:`zip(iterA, iterB, ...) <zip>` takes one element from each iterable "
"and returns them in a tuple::"
msgstr ""
":func:`zip(iterA, iterB, ...) <zip>` はそれぞれの iterable "
"から1つの要素を取り、それらをタプルに入れて返します::"
#: ../../howto/functional.rst:718
msgid ""
"It doesn't construct an in-memory list and exhaust all the input iterators "
"before returning; instead tuples are constructed and returned only if "
"they're requested. (The technical term for this behaviour is `lazy "
"evaluation <https://en.wikipedia.org/wiki/Lazy_evaluation>`__.)"
msgstr ""
":func:`zip` は結果を返す前に入力のイテレーターを全て消費してメモリ上に list を作成しません。代わりに要求されるたびに tuple "
"を生成して返します。(この動作を技術的な用語で `lazy evaluation "
"<https://en.wikipedia.org/wiki/Lazy_evaluation>`__ (訳: `遅延評価 "
"<https://ja.wikipedia.org/wiki/%E9%81%85%E5%BB%B6%E8%A9%95%E4%BE%A1>`__) "
"と呼びます。)"
#: ../../howto/functional.rst:723
msgid ""
"This iterator is intended to be used with iterables that are all of the same"
" length. If the iterables are of different lengths, the resulting stream "
"will be the same length as the shortest iterable. ::"
msgstr ""
"このイテレータの用途には、すべて同じ長さのイテラブルを想定しています。長さが違っていれば、出力されるストリームは一番短いイテラブルと同じ長さになります。"
" ::"