GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — python-docs-ja/tutorial/classes.po at 3.15 · python/python-docs-ja Expand file tree/ Copy pathclasses.poMore file actions 2038 lines (1884 loc) · 92.1 KB / Copy pathclasses.poFile metadata and controls2038 lines (1884 loc) · 92.1 KB 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 Python Software Foundation # This file is distributed under the same license as the Python package. # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR. # # Translators: # python-doc bot, 2025 # Rafael Fontenelle <[email protected]>, 2025 # Takanori Suzuki <[email protected]>, 2025 # TENMYO Masakazu, 2026 # Inada Naoki <[email protected]>, 2026 # #, fuzzy msgid "" msgstr "" "Project-Id-Version: Python 3.15\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2026-10-23 18:01+0000\n" "PO-Revision-Date: 2025-09-16 00:02+0000\n" "Last-Translator: Inada Naoki <[email protected]>, 2026\n" "Language-Team: Japanese (https://app.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" #: ../../tutorial/classes.rst:5 msgid "Classes" msgstr "クラス" #: ../../tutorial/classes.rst:7 msgid "" "Classes provide a means of bundling data and functionality together. " "Creating a new class creates a new *type* of object, allowing new " "*instances* of that type to be made. Each class instance can have " "attributes attached to it for maintaining its state. Class instances can " "also have methods (defined by its class) for modifying its state." msgstr "" "クラスはデータと機能を組み合わせる方法を提供します。\n" "新規にクラスを作成することで、新しいオブジェクトの *型* を作成し、その型を持" "つ新しい *インスタンス* が作れます。\n" "クラスのそれぞれのインスタンスは自身の状態を保持する属性を持てます。\n" "クラスのインスタンスは、その状態を変更するための (そのクラスが定義する) メ" "ソッドも持てます。" msgid "" "Compared with other programming languages, Python's class mechanism adds " "classes with a minimum of new syntax and semantics. It is a mixture of the " "class mechanisms found in C++ and Modula-3. Python classes provide all the " "standard features of Object Oriented Programming: the class inheritance " "mechanism allows multiple base classes, a derived class can override any " "methods of its base class or classes, and a method can call the method of a " "base class with the same name. Objects can contain arbitrary amounts and " "kinds of data. As is true for modules, classes partake of the dynamic " "nature of Python: they are created at runtime, and can be modified further " "after creation." msgstr "" "Python のクラス機構は、ほかのプログラミング言語に比べて、新しく覚える構文や規" "則を最小限に抑えています。\n" "その仕組みは C++ と Modula-3 のクラス機構を組み合わせたものです。\n" "Python のクラスは、オブジェクト指向プログラミングの標準的な機能をすべて備えて" "います。\n" "複数の基底クラスを継承でき、派生クラスでは基底クラスのどのメソッドもオーバー" "ライドできます。\n" "また、メソッドから基底クラスの同名のメソッドを呼び出せます。\n" "オブジェクトに格納するデータの種類や数も自由です。\n" "モジュールと同様に、クラスも Python の動的な性質を持っています。\n" "クラスは実行時に作られ、作成後に変更することもできます。" msgid "" "In C++ terminology, normally class members (including the data members) are " "*public* (except see below :ref:`tut-private`), and all member functions are " "*virtual*. As in Modula-3, there are no shorthands for referencing the " "object's members from its methods: the method function is declared with an " "explicit first argument representing the object, which is provided " "implicitly by the call. As in Smalltalk, classes themselves are objects. " "This provides semantics for importing and renaming. Unlike C++ and " "Modula-3, built-in types can be used as base classes for extension by the " "user. Also, like in C++, most built-in operators with special syntax " "(arithmetic operators, subscripting etc.) can be redefined for class " "instances." msgstr "" "C++ の用語で言うと、クラスメンバはデータメンバも含めて通常 *public* です\n" "(例外については :ref:`tut-private` を参照してください)。\n" "メンバ関数はすべて *仮想関数 (virtual)* です。\n" "Modula-3 と同様に、メソッド内からオブジェクトのメンバを参照するための短縮記法" "はありません。\n" "メソッド関数の宣言では、オブジェクト自体を表す引数を最初に明示します。\n" "メソッドを呼び出すと、この引数にオブジェクトが暗黙的に渡されます。\n" "Smalltalk と同様に、クラス自体もオブジェクトなので、インポートしたり別の名前" "を付けたりできます。\n" "C++ や Modula-3 と違って、組み込み型を基底クラスにして拡張することもできま" "す。\n" "また、C++ と同様に、算術演算子や添字表記など特別な構文を使う組み込み演算子の" "多くは、\n" "クラスインスタンスに対する動作を再定義できます。" #: ../../tutorial/classes.rst:34 msgid "" "(Lacking universally accepted terminology to talk about classes, I will make " "occasional use of Smalltalk and C++ terms. I would use Modula-3 terms, " "since its object-oriented semantics are closer to those of Python than C++, " "but I expect that few readers have heard of it.)" msgstr "" "(クラスに関して普遍的な用語定義がないので、 Smalltalk と C++ の用語を場合に応" "じて使っていくことにします。 C++ よりも Modula-3 の方がオブジェクト指向の意味" "論が Python に近いので、 Modula-3 の用語を使いたいのですが、ほとんどの読者は " "Modula-3 についてしらないでしょうから。)" #: ../../tutorial/classes.rst:43 msgid "A Word About Names and Objects" msgstr "名前とオブジェクトについて" msgid "" "Objects have individuality, and multiple names (in multiple scopes) can be " "bound to the same object. This is known as aliasing in other languages. " "This is usually not appreciated on a first glance at Python, and can be " "safely ignored when dealing with immutable basic types (numbers, strings, " "tuples). However, aliasing has a possibly surprising effect on the " "semantics of Python code involving mutable objects such as lists, " "dictionaries, and most other types. This is usually used to the benefit of " "the program, since aliases behave like pointers in some respects. For " "example, passing an object is cheap since only a pointer is passed by the " "implementation; and if a function modifies an object passed as an argument, " "the caller will see the change --- this eliminates the need for two " "different argument passing mechanisms as in Pascal." msgstr "" "オブジェクトはそれぞれ固有の存在ですが、同じオブジェクトに複数のスコープから" "複数の名前を束縛できます。\n" "ほかの言語では、これをエイリアス (別名) と呼びます。\n" "Python を学び始めたばかりでは、この仕組みの重要性は分かりにくいかもしれませ" "ん。\n" "数値、文字列、タプルのようなイミュータブル (変更できない) な基本型を扱うとき" "は、気にしなくても問題ありません。\n" "一方、リストや辞書など多くの型のミュータブル (変更できる) なオブジェクトを扱" "うコードでは、\n" "エイリアスによって意外な動作が起こることがあります。\n" "エイリアスはいくつかの点でポインタのように振る舞うため、通常はその性質をプロ" "グラムに役立てられます。\n" "例えば、オブジェクトを渡す際に実装上渡されるのはポインタだけなので、処理の負" "担は小さくなります。\n" "また、関数が引数として受け取ったオブジェクトを変更すると、呼び出し側からもそ" "の変更が見えます。\n" "このため、Pascal のように引数の渡し方を 2 種類用意する必要がありません。" #: ../../tutorial/classes.rst:61 msgid "Python Scopes and Namespaces" msgstr "Python のスコープと名前空間" msgid "" "Before introducing classes, I first have to tell you something about " "Python's scope rules. Class definitions play some neat tricks with " "namespaces, and you need to know how scopes and namespaces work to fully " "understand what's going on. Incidentally, knowledge about this subject is " "useful for any advanced Python programmer." msgstr "" "クラスを紹介する前に、Python のスコープの規則について説明する必要がありま" "す。\n" "クラス定義は名前空間を巧みに利用するので、その動作を十分に理解するには、ス" "コープと名前空間の仕組みを知っておく必要があります。\n" "なお、この知識は高度なプログラミングを行う Python プログラマーなら誰にとって" "も役立ちます。" #: ../../tutorial/classes.rst:69 msgid "Let's begin with some definitions." msgstr "まず定義から始めましょう。" #: ../../tutorial/classes.rst:71 msgid "" "A *namespace* is a mapping from names to objects. Most namespaces are " "currently implemented as Python dictionaries, but that's normally not " "noticeable in any way (except for performance), and it may change in the " "future. Examples of namespaces are: the set of built-in names (containing " "functions such as :func:`abs`, and built-in exception names); the global " "names in a module; and the local names in a function invocation. In a sense " "the set of attributes of an object also form a namespace. The important " "thing to know about namespaces is that there is absolutely no relation " "between names in different namespaces; for instance, two different modules " "may both define a function ``maximize`` without confusion --- users of the " "modules must prefix it with the module name." msgstr "" "*名前空間 (namespace)* とは、名前からオブジェクトへの対応付け (mapping) で" "す。ほとんどの名前空間は、現状では Python の辞書として実装されていますが、そ" "のことは通常は (パフォーマンス以外では) 目立つことはないし、将来は変更される" "かもしれません。名前空間の例には、組込み名の集合 (:func:`abs` 等の関数や組込" "み例外名)、モジュール内のグローバルな名前、関数を呼び出したときのローカルな名" "前があります。オブジェクトの属性からなる集合もまた、ある意味では名前空間で" "す。名前空間について知っておくべき重要なことは、異なった名前空間にある名前の" "間には全く関係がないということです。例えば、二つの別々のモジュールの両方で関" "数 ``maximize`` という関数を定義することができ、定義自体は混同されることはあ" "りません --- モジュールのユーザは名前の前にモジュール名をつけなければなりませ" "ん。" #: ../../tutorial/classes.rst:82 msgid "" "By the way, I use the word *attribute* for any name following a dot --- for " "example, in the expression ``z.real``, ``real`` is an attribute of the " "object ``z``. Strictly speaking, references to names in modules are " "attribute references: in the expression ``modname.funcname``, ``modname`` is " "a module object and ``funcname`` is an attribute of it. In this case there " "happens to be a straightforward mapping between the module's attributes and " "the global names defined in the module: they share the same namespace! [#]_" msgstr "" "ところで、 *属性* という言葉は、ドットに続く名前すべてに対して使っています " "--- 例えば式 ``z.real`` で、 ``real`` はオブジェクト ``z`` の属性です。厳密に" "いえば、モジュール内の名前に対する参照は属性の参照です。式 " "``modname.funcname`` では、 ``modname`` はあるモジュールオブジェクトで、 " "``funcname`` はその属性です。この場合には、モジュールの属性とモジュールの中で" "定義されているグローバル名の間には、直接的な対応付けがされます。これらの名前" "は同じ名前空間を共有しているのです! [#]_" #: ../../tutorial/classes.rst:90 msgid "" "Attributes may be read-only or writable. In the latter case, assignment to " "attributes is possible. Module attributes are writable: you can write " "``modname.the_answer = 42``. Writable attributes may also be deleted with " "the :keyword:`del` statement. For example, ``del modname.the_answer`` will " "remove the attribute :attr:`!the_answer` from the object named by " "``modname``." msgstr "" "属性は読取り専用にも、書込み可能にもできます。書込み可能であれば、属性に代入" "することができます。モジュール属性は書込み可能で、 ``modname.the_answer = " "42`` と書くことができます。書込み可能な属性は、 :keyword:`del` 文で削除するこ" "ともできます。例えば、 ``del modname.the_answer`` は、 ``modname`` で指定され" "たオブジェクトから属性 :attr:`!the_answer` を除去します。" #: ../../tutorial/classes.rst:96 msgid "" "Namespaces are created at different moments and have different lifetimes. " "The namespace containing the built-in names is created when the Python " "interpreter starts up, and is never deleted. The global namespace for a " "module is created when the module definition is read in; normally, module " "namespaces also last until the interpreter quits. The statements executed " "by the top-level invocation of the interpreter, either read from a script " "file or interactively, are considered part of a module " "called :mod:`__main__`, so they have their own global namespace. (The built-" "in names actually also live in a module; this is called :mod:`builtins`.)" msgstr "" "名前空間は様々な時点で作成され、その寿命も様々です。組み込みの名前が入った名" "前空間は Python インタプリタが起動するときに作成され、決して削除されることは" "ありません。モジュールのグローバルな名前空間は、モジュール定義が読み込まれた" "ときに作成されます。通常、モジュールの名前空間は、インタプリタが終了するまで" "残ります。インタプリタのトップレベルで実行された文は、スクリプトファイルから" "読み出されたものでも対話的に読み出されたものでも、 :mod:`__main__` という名前" "のモジュールの一部分であるとみなされるので、独自の名前空間を持つことになりま" "す。 (組み込みの名前は実際にはモジュール内に存在します。そのモジュール" "は :mod:`builtins` と呼ばれています。)" #: ../../tutorial/classes.rst:106 msgid "" "The local namespace for a function is created when the function is called, " "and deleted when the function returns or raises an exception that is not " "handled within the function. (Actually, forgetting would be a better way to " "describe what actually happens.) Of course, recursive invocations each have " "their own local namespace." msgstr "" "関数のローカルな名前空間は、関数が呼び出されたときに作成され、関数から戻った" "ときや、関数内で例外が送出され、かつ関数内で処理されなかった場合に削除されま" "す。 (実際には、忘れられる、と言ったほうが起きていることをよく表していま" "す。) もちろん、再帰呼出しのときには、各々の呼び出しで各自のローカルな名前空" "間があります。" #: ../../tutorial/classes.rst:112 msgid "" "A *scope* is a textual region of a Python program where a namespace is " "directly accessible. \"Directly accessible\" here means that an unqualified " "reference to a name attempts to find the name in the namespace." msgstr "" "*スコープ (scope)* とは、ある名前空間が直接アクセスできるような、 Python プロ" "グラムのテキスト上の領域です。 \"直接アクセス可能\" とは、修飾なしに (訳注: " "``spam.egg`` ではなく単に ``egg`` のように) 名前を参照した際に、その名前空間" "から名前を見つけようと試みることを意味します。" #: ../../tutorial/classes.rst:116 msgid "" "Although scopes are determined statically, they are used dynamically. At any " "time during execution, there are 3 or 4 nested scopes whose namespaces are " "directly accessible:" msgstr "" "スコープは静的に決定されますが、動的に使用されます。実行中はいつでも、直接名" "前空間にアクセス可能な、3つまたは4つの入れ子になったスコープがあります:" #: ../../tutorial/classes.rst:120 msgid "the innermost scope, which is searched first, contains the local names" msgstr "最初に探される、最も内側のスコープは、ローカルな名前を持っています。" #: ../../tutorial/classes.rst:121 msgid "" "the scopes of any enclosing functions, which are searched starting with the " "nearest enclosing scope, contain non-local, but also non-global names" msgstr "" "外側の(enclosing)関数のスコープは、近いほうから順に探され、ローカルでもグロー" "バルでもない名前を持っています。" msgid "the next-to-last scope contains the current module's global names" msgstr "" "最後から 2 番目のスコープは現在のモジュールのグローバルな名前を持っています。" #: ../../tutorial/classes.rst:124 msgid "" "the outermost scope (searched last) is the namespace containing built-in " "names" msgstr "一番外側の(最後に検索される)スコープはビルトイン名を持っています。" msgid "" "If a name is declared global, then all references and assignments go " "directly to the next-to-last scope containing the module's global names. To " "rebind variables found outside of the innermost scope, " "the :keyword:`nonlocal` statement can be used; if not declared nonlocal, " "those variables are read-only (an attempt to write to such a variable will " "simply create a *new* local variable in the innermost scope, leaving the " "identically named outer variable unchanged)." msgstr "" "名前を global と宣言すると、その名前への参照と代入はすべて、モジュールのグ" "ローバル名を持つ最後から 2 番目のスコープで直接行われます。\n" "最も内側のスコープより外側にある変数を再束縛するには、:keyword:`nonlocal` 文" "を使えます。\n" "こうした外側の変数は、nonlocal と宣言しない限り読み出し専用です。\n" "代入しようとすると、最も内側のスコープに同名の *新しい* ローカル変数が作ら" "れ、外側の変数は変わりません。" #: ../../tutorial/classes.rst:133 msgid "" "Usually, the local scope references the local names of the (textually) " "current function. Outside functions, the local scope references the same " "namespace as the global scope: the module's namespace. Class definitions " "place yet another namespace in the local scope." msgstr "" "通常、ローカルスコープは (プログラムテキスト上の) 現在の関数のローカルな名前" "を参照します。関数の外側では、ローカルスコープはグローバルな名前空間と同じ名" "前空間、モジュールの名前空間を参照します。クラス定義では、ローカルスコープの" "中にもう一つ名前空間が置かれます。" #: ../../tutorial/classes.rst:138 msgid "" "It is important to realize that scopes are determined textually: the global " "scope of a function defined in a module is that module's namespace, no " "matter from where or by what alias the function is called. On the other " "hand, the actual search for names is done dynamically, at run time --- " "however, the language definition is evolving towards static name resolution, " "at \"compile\" time, so don't rely on dynamic name resolution! (In fact, " "local variables are already determined statically.)" msgstr "" "スコープはテキスト上で決定されていると理解することが重要です。モジュール内で" "定義される関数のグローバルなスコープは、関数がどこから呼び出されても、どんな" "別名をつけて呼び出されても、そのモジュールの名前空間になります。反対に、実際" "の名前の検索は実行時に動的に行われます --- とはいえ、言語の定義は、\"コンパイ" "ル\" 時の静的な名前解決の方向に進化しているので、動的な名前解決に頼ってはいけ" "ません! (事実、ローカルな変数は既に静的に決定されています。)" msgid "" "A special quirk of Python is that -- if no :keyword:`global` " "or :keyword:`nonlocal` statement is in effect -- assignments to names always " "go into the innermost scope. Assignments do not copy data --- they just bind " "names to objects. The same is true for deletions: the statement ``del x`` " "removes the binding of ``x`` from the namespace referenced by the local " "scope. In fact, all operations that introduce new names use the local " "scope: in particular, :keyword:`import` statements and function definitions " "bind the module or function name in the local scope." msgstr "" ":keyword:`global` 文や :keyword:`nonlocal` 文が有効でない場合、\n" "Python では名前への代入は常に最も内側のスコープで行われます。\n" "代入はデータをコピーせず、名前をオブジェクトに束縛するだけです。\n" "削除も同様で、``del x`` はローカルスコープの名前空間から ``x`` の束縛を削除し" "ます。\n" "新しい名前を導入する操作はすべてローカルスコープを使います。\n" "例えば :keyword:`import` 文や関数定義は、モジュール名や関数名をローカルスコー" "プで束縛します。" #: ../../tutorial/classes.rst:154 msgid "" "The :keyword:`global` statement can be used to indicate that particular " "variables live in the global scope and should be rebound there; " "the :keyword:`nonlocal` statement indicates that particular variables live " "in an enclosing scope and should be rebound there." msgstr "" ":keyword:`global` 文を使うと、特定の変数がグローバルスコープに存在し、そこで" "再束縛されることを指示できます。 :keyword:`nonlocal` 文は、特定の変数が外側の" "スコープに存在し、そこで再束縛されることを指示します。" #: ../../tutorial/classes.rst:162 msgid "Scopes and Namespaces Example" msgstr "スコープと名前空間の例" #: ../../tutorial/classes.rst:164 msgid "" "This is an example demonstrating how to reference the different scopes and " "namespaces, and how :keyword:`global` and :keyword:`nonlocal` affect " "variable binding::" msgstr "" "異なるスコープと名前空間がどのように参照されるか、また :keyword:`global` およ" "び :keyword:`nonlocal` が変数の束縛にどう影響するか、この例で実演します::" #: ../../tutorial/classes.rst:168 msgid "" "def scope_test():\n" " def do_local():\n" " spam = \"local spam\"\n" "\n" " def do_nonlocal():\n" " nonlocal spam\n" " spam = \"nonlocal spam\"\n" "\n" " def do_global():\n" " global spam\n" " spam = \"global spam\"\n" "\n" " spam = \"test spam\"\n" " do_local()\n" " print(\"After local assignment:\", spam)\n" " do_nonlocal()\n" " print(\"After nonlocal assignment:\", spam)\n" " do_global()\n" " print(\"After global assignment:\", spam)\n" "\n" "scope_test()\n" "print(\"In global scope:\", spam)" msgstr "" "def scope_test():\n" " def do_local():\n" " spam = \"local spam\"\n" "\n" " def do_nonlocal():\n" " nonlocal spam\n" " spam = \"nonlocal spam\"\n" "\n" " def do_global():\n" " global spam\n" " spam = \"global spam\"\n" "\n" " spam = \"test spam\"\n" " do_local()\n" " print(\"After local assignment:\", spam)\n" " do_nonlocal()\n" " print(\"After nonlocal assignment:\", spam)\n" " do_global()\n" " print(\"After global assignment:\", spam)\n" "\n" "scope_test()\n" "print(\"In global scope:\", spam)" #: ../../tutorial/classes.rst:191 msgid "The output of the example code is:" msgstr "このコード例の出力は:" #: ../../tutorial/classes.rst:193 msgid "" "After local assignment: test spam\n" "After nonlocal assignment: nonlocal spam\n" "After global assignment: nonlocal spam\n" "In global scope: global spam" msgstr "" "After local assignment: test spam\n" "After nonlocal assignment: nonlocal spam\n" "After global assignment: nonlocal spam\n" "In global scope: global spam" #: ../../tutorial/classes.rst:200 msgid "" "Note how the *local* assignment (which is default) didn't change " "*scope_test*\\'s binding of *spam*. The :keyword:`nonlocal` assignment " "changed *scope_test*\\'s binding of *spam*, and the :keyword:`global` " "assignment changed the module-level binding." msgstr "" "このとおり、(デフォルトの) *ローカルな* 代入は *scope_test* 上の *spam* への" "束縛を変更しませんでした。 :keyword:`nonlocal` 代入は *scope_test* 上の " "*spam* への束縛を変更し、 :keyword:`global` 代入はモジュールレベルの束縛を変" "更しました。" msgid "" "You can also see that there was no previous binding for *spam* before " "the :keyword:`global` assignment." msgstr "" "また、:keyword:`global` による代入の前には、グローバルスコープで *spam* がま" "だ束縛されていなかったことも分かります。" #: ../../tutorial/classes.rst:212 msgid "A First Look at Classes" msgstr "クラス初見" #: ../../tutorial/classes.rst:214 msgid "" "Classes introduce a little bit of new syntax, three new object types, and " "some new semantics." msgstr "" "クラスでは、新しい構文を少しと、三つの新たなオブジェクト型、そして新たな意味" "付けをいくつか取り入れています。" #: ../../tutorial/classes.rst:221 msgid "Class Definition Syntax" msgstr "クラス定義の構文" #: ../../tutorial/classes.rst:223 msgid "The simplest form of class definition looks like this::" msgstr "クラス定義の最も単純な形式は、次のようになります::" #: ../../tutorial/classes.rst:225 msgid "" "class ClassName:\n" " <statement-1>\n" " .\n" " .\n" " .\n" " <statement-N>" msgstr "" "class ClassName:\n" " <statement-1>\n" " .\n" " .\n" " .\n" " <statement-N>" #: ../../tutorial/classes.rst:232 msgid "" "Class definitions, like function definitions (:keyword:`def` statements) " "must be executed before they have any effect. (You could conceivably place " "a class definition in a branch of an :keyword:`if` statement, or inside a " "function.)" msgstr "" "関数定義 (:keyword:`def` 文) と同様、クラス定義が効果をもつにはまず実行しなけ" "ればなりません。 (クラス定義を :keyword:`if` 文の分岐先や関数内部に置くこと" "も、考え方としてはありえます。)" #: ../../tutorial/classes.rst:236 msgid "" "In practice, the statements inside a class definition will usually be " "function definitions, but other statements are allowed, and sometimes useful " "--- we'll come back to this later. The function definitions inside a class " "normally have a peculiar form of argument list, dictated by the calling " "conventions for methods --- again, this is explained later." msgstr "" "実際には、クラス定義の内側にある文は、通常は関数定義になりますが、他の文を書" "くこともでき、それが役に立つこともあります --- これについては後で述べます。ク" "ラス内の関数定義は通常、メソッドの呼び出し規約で決められた独特の形式の引数リ" "ストを持ちます --- これについても後で述べます。" msgid "" "When a class definition is entered, a new namespace is created, and used as " "the local scope --- thus, all assignments to local variables go into this " "new namespace. In particular, function definitions bind the name of the new " "function here." msgstr "" "クラス定義に入ると、新しい名前空間が作られ、ローカルスコープとして使われま" "す。\n" "したがって、ローカル変数への代入はすべてこの新しい名前空間に対して行われま" "す。\n" "特に、関数定義では新しい関数の名前がここに束縛されます。" #: ../../tutorial/classes.rst:247 msgid "" "When a class definition is left normally (via the end), a *class object* is " "created. This is basically a wrapper around the contents of the namespace " "created by the class definition; we'll learn more about class objects in the " "next section. The original local scope (the one in effect just before the " "class definition was entered) is reinstated, and the class object is bound " "here to the class name given in the class definition header (:class:`!" "ClassName` in the example)." msgstr "" "クラス定義から普通に (定義の終端に到達して) 抜けると、 *クラスオブジェクト " "(class object)* が生成されます。クラスオブジェクトは、基本的にはクラス定義で" "作成された名前空間の内容をくるむラッパー (wrapper) です。クラスオブジェクトに" "ついては次の節で詳しく学ぶことにします。 (クラス定義に入る前に有効だった) 元" "のローカルスコープが復帰し、生成されたクラスオブジェクトは復帰したローカルス" "コープにクラス定義のヘッダで指定した名前 (上の例では :class:`!ClassName`) で" "結び付けられます。" #: ../../tutorial/classes.rst:259 msgid "Class Objects" msgstr "クラスオブジェクト" #: ../../tutorial/classes.rst:261 msgid "" "Class objects support two kinds of operations: attribute references and " "instantiation." msgstr "" "クラスオブジェクトでは2種類の演算、属性参照とインスタンス生成をサポートして" "います。" #: ../../tutorial/classes.rst:264 msgid "" "*Attribute references* use the standard syntax used for all attribute " "references in Python: ``obj.name``. Valid attribute names are all the names " "that were in the class's namespace when the class object was created. So, " "if the class definition looked like this::" msgstr "" "*属性参照 (attribute reference)* は、Python におけるすべての属性参照で使われ" "ている標準的な構文、 ``obj.name`` を使います。クラスオブジェクトが生成された" "際にクラスの名前空間にあった名前すべてが有効な属性名です。従って、以下のよう" "なクラス定義では::" #: ../../tutorial/classes.rst:269 msgid "" "class MyClass:\n" " \"\"\"A simple example class\"\"\"\n" " i = 12345\n" "\n" " def f(self):\n" " return 'hello world'" msgstr "" "class MyClass:\n" " \"\"\"A simple example class\"\"\"\n" " i = 12345\n" "\n" " def f(self):\n" " return 'hello world'" #: ../../tutorial/classes.rst:276 msgid "" "then ``MyClass.i`` and ``MyClass.f`` are valid attribute references, " "returning an integer and a function object, respectively. Class attributes " "can also be assigned to, so you can change the value of ``MyClass.i`` by " "assignment. :attr:`~type.__doc__` is also a valid attribute, returning the " "docstring belonging to the class: ``\"A simple example class\"``." msgstr "" "``MyClass.i`` と ``MyClass.f`` は有効な属性参照であり、それぞれ整数と関数オブ" "ジェクトを返します。クラス属性も代入できるため、代入により ``MyClass.i`` の値" "を変えられます。 :attr:`~type.__doc__` も有効な属性で、そのクラスに属している" "ドキュメンテーション文字列(docstring) 、この場合は ``\"A simple example " "class\"`` を返します。" #: ../../tutorial/classes.rst:282 msgid "" "Class *instantiation* uses function notation. Just pretend that the class " "object is a parameterless function that returns a new instance of the class. " "For example (assuming the above class)::" msgstr "" "クラスの *インスタンス化 (instantiation)* には関数記法を使います。クラスオブ" "ジェクトのことを、クラスの新しいインスタンスを返す、引数のない関数のように" "扱ってください。上記クラスで例示すると::" #: ../../tutorial/classes.rst:286 ../../tutorial/classes.rst:303 msgid "x = MyClass()" msgstr "x = MyClass()" #: ../../tutorial/classes.rst:288 msgid "" "creates a new *instance* of the class and assigns this object to the local " "variable ``x``." msgstr "" "は、クラスの新しい *インスタンス (instance)* を生成し、そのオブジェクトをロー" "カル変数 ``x`` へ代入します。" #: ../../tutorial/classes.rst:291 msgid "" "The instantiation operation (\"calling\" a class object) creates an empty " "object. Many classes like to create objects with instances customized to a " "specific initial state. Therefore a class may define a special method " "named :meth:`~object.__init__`, like this::" msgstr "" "インスタンス化操作 (クラスオブジェクトの \"呼出し\") では、空のオブジェクトが" "作られます。多くのクラスでは、特定の初期状態にカスタマイズされたオブジェクト" "を作りたいです。そのために、クラスには :meth:`~object.__init__` という名前の" "特殊メソッドを定義できます。例えば次のようにします::" #: ../../tutorial/classes.rst:296 msgid "" "def __init__(self):\n" " self.data = []" msgstr "" "def __init__(self):\n" " self.data = []" #: ../../tutorial/classes.rst:299 msgid "" "When a class defines an :meth:`~object.__init__` method, class instantiation " "automatically invokes :meth:`!__init__` for the newly created class " "instance. So in this example, a new, initialized instance can be obtained " "by::" msgstr "" "クラスが :meth:`~object.__init__` メソッドを定義している場合、クラスをインス" "タンス化すると、新しく作られたクラスインスタンスに対して自動的に :meth:`!" "__init__` を呼び出します。 従って この例では、新たな初期済みインスタンスを次" "のようにして得られます::" #: ../../tutorial/classes.rst:305 msgid "" "Of course, the :meth:`~object.__init__` method may have arguments for " "greater flexibility. In that case, arguments given to the class " "instantiation operator are passed on to :meth:`!__init__`. For example, ::" msgstr "" "もちろん、より大きな柔軟性を持たせるために、 :meth:`~object.__init__` メソッ" "ドに複数の引数をもたせることができます。その場合、次の例のように、クラスのイ" "ンスタンス化操作に渡された引数は :meth:`!__init__` に渡されます。例えば、 ::" #: ../../tutorial/classes.rst:309 msgid "" ">>> class Complex:\n" "... def __init__(self, realpart, imagpart):\n" "... self.r = realpart\n" "... self.i = imagpart\n" "...\n" ">>> x = Complex(3.0, -4.5)\n" ">>> x.r, x.i\n" "(3.0, -4.5)" msgstr "" ">>> class Complex:\n" "... def __init__(self, realpart, imagpart):\n" "... self.r = realpart\n" "... self.i = imagpart\n" "...\n" ">>> x = Complex(3.0, -4.5)\n" ">>> x.r, x.i\n" "(3.0, -4.5)" #: ../../tutorial/classes.rst:322 msgid "Instance Objects" msgstr "インスタンスオブジェクト" #: ../../tutorial/classes.rst:324 msgid "" "Now what can we do with instance objects? The only operations understood by " "instance objects are attribute references. There are two kinds of valid " "attribute names: data attributes and methods." msgstr "" "ところで、インスタンスオブジェクトを使うと何ができるのでしょうか?インスタン" "スオブジェクトが理解できる唯一の操作は、属性の参照です。有効な属性名には " "(データ属性およびメソッドの) 二種類あります。" msgid "" "*Data attributes* correspond to \"instance variables\" in Smalltalk, and to " "\"data members\" in C++. Data attributes need not be declared; like local " "variables, they spring into existence when they are first assigned to. For " "example, if ``x`` is the instance of :class:`!MyClass` created above, the " "following piece of code will print the value ``16``, without leaving a " "trace::" msgstr "" "*データ属性 (data attribute)* は Smalltalk の「インスタンス変数」や C++ の" "「データメンバ」に相当します。\n" "データ属性を宣言する必要はありません。\n" "ローカル変数と同様に、初めて値を代入したときに作られます。\n" "例えば、``x`` が上で作成した :class:`!MyClass` のインスタンスなら、\n" "次のコードは値 ``16`` を表示した後、追加した ``counter`` 属性を削除しま" "す。 ::" #: ../../tutorial/classes.rst:334 msgid "" "x.counter = 1\n" "while x.counter < 10:\n" " x.counter = x.counter * 2\n" "print(x.counter)\n" "del x.counter" msgstr "" "x.counter = 1\n" "while x.counter < 10:\n" " x.counter = x.counter * 2\n" "print(x.counter)\n" "del x.counter" #: ../../tutorial/classes.rst:340 msgid "" "The other kind of instance attribute reference is a *method*. A method is a " "function that \"belongs to\" an object." msgstr "" "もう一つのインスタンス属性の参照は、 *メソッド* です。メソッドとは、オブジェ" "クトに \"属する\" 関数のことです。" msgid "" "Valid method names of an instance object depend on its class. By " "definition, all attributes of a class that are function objects define " "corresponding methods of its instances. So in our example, ``x.f`` is a " "valid method reference, since ``MyClass.f`` is a function, but ``x.i`` is " "not, since ``MyClass.i`` is not. But ``x.f`` is not the same thing as " "``MyClass.f`` --- it is a *method object*, not a function object." msgstr "" "インスタンスオブジェクトで有効なメソッド名は、そのクラスによって決まりま" "す。\n" "定義上、クラス属性が関数オブジェクトであれば、そのクラスのインスタンスに対応" "するメソッドが定義されます。\n" "この例では ``MyClass.f`` が関数なので、``x.f`` は有効なメソッド参照です。\n" "一方、``MyClass.i`` は関数ではないので、``x.i`` はメソッド参照ではありませ" "ん。\n" "ただし、``x.f`` は ``MyClass.f`` と同じものではありません。\n" "``x.f`` は関数オブジェクトではなく、*メソッドオブジェクト (method object)* で" "す。" #: ../../tutorial/classes.rst:356 msgid "Method Objects" msgstr "メソッドオブジェクト" #: ../../tutorial/classes.rst:358 msgid "Usually, a method is called right after it is bound::" msgstr "普通、メソッドはバインドされた直後に呼び出されます::" #: ../../tutorial/classes.rst:360 msgid "x.f()" msgstr "x.f()" #: ../../tutorial/classes.rst:362 msgid "" "If ``x = MyClass()``, as above, this will return the string ``'hello " "world'``. However, it is not necessary to call a method right away: ``x.f`` " "is a method object, and can be stored away and called at a later time. For " "example::" msgstr "" "もし ``x = MyClass()`` であれば、上記のコードは文字列 ``'hello world'`` を返" "すでしょう。しかしながら、必ずしもメソッドをその場で呼び出さなければならない" "わけではありません。 ``x.f`` はメソッドオブジェクトであり、どこかに記憶してお" "いて後で呼び出すことができます。例えば次のコードは::" #: ../../tutorial/classes.rst:366 msgid "" "xf = x.f\n" "while True:\n" " print(xf())" msgstr "" "xf = x.f\n" "while True:\n" " print(xf())" #: ../../tutorial/classes.rst:370 msgid "will continue to print ``hello world`` until the end of time." msgstr "``hello world`` を時が終わるまで印字し続けるでしょう。" #: ../../tutorial/classes.rst:372 msgid "" "What exactly happens when a method is called? You may have noticed that " "``x.f()`` was called without an argument above, even though the function " "definition for :meth:`!f` specified an argument. What happened to the " "argument? Surely Python raises an exception when a function that requires an " "argument is called without any --- even if the argument isn't actually " "used..." msgstr "" "メソッドが呼び出されるときには実際には何が起きているのでしょうか? :meth:`!" "f` の関数定義では引数を一つ指定していたにもかかわらず、上の例では ``x.f()`` " "が引数なしで呼び出されています。引数はどうなったのでしょうか?たしか、引数が" "必要な関数を引数無しで呼び出すと、 Python が例外を送出するはずです --- たとえ" "その引数が実際には使われなくても…。" #: ../../tutorial/classes.rst:378 msgid "" "Actually, you may have guessed the answer: the special thing about methods " "is that the instance object is passed as the first argument of the " "function. In our example, the call ``x.f()`` is exactly equivalent to " "``MyClass.f(x)``. In general, calling a method with a list of *n* arguments " "is equivalent to calling the corresponding function with an argument list " "that is created by inserting the method's instance object before the first " "argument." msgstr "" "もう答は想像できているかもしれませんね:\n" "メソッドについて特別なこととして、インスタンスオブジェクトが関数の第1引数とし" "て渡されます。\n" "例では、 ``x.f()`` という呼び出しは、 ``MyClass.f(x)`` と厳密に等価なもので" "す。\n" "一般に、 *n* 個の引数リストもったメソッドの呼出しは、そのメソッドのインスタン" "スオブジェクトを最初の引数の前に挿入した引数リストで、メソッドに対応する関数" "を呼び出すことと等価です。" msgid "" "In general, methods work as follows. When a non-data attribute of an " "instance is referenced, the instance's class is searched. If the name " "denotes a valid class attribute that is a function object, references to " "both the instance object and the function object are packed into a method " "object. When the method object is called with an argument list, a new " "argument list is constructed from the instance object and the argument list, " "and the function object is called with this new argument list." msgstr "" "メソッドは一般に次のように動作します。\n" "インスタンスの非データ属性を参照すると、そのインスタンスのクラスから属性を探" "します。\n" "見つかった属性が関数オブジェクトなら、インスタンスと関数の両方への参照をまと" "めてメソッドオブジェクトにします。\n" "そのメソッドを呼び出すと、渡された引数の先頭にインスタンスを加えた新しい引数" "リストが作られます。\n" "そして、その引数リストで関数を呼び出します。" msgid "Class and Instance Variables" msgstr "クラス変数とインスタンス変数" #: ../../tutorial/classes.rst:400 msgid "" "Generally speaking, instance variables are for data unique to each instance " "and class variables are for attributes and methods shared by all instances " "of the class::" msgstr "" "一般的に、インスタンス変数はそれぞれのインスタンスについて固有のデータのため" "のもので、クラス変数はそのクラスのすべてのインスタンスによって共有される属性" "やメソッドのためのものです::" #: ../../tutorial/classes.rst:404 msgid "" "class Dog:\n" "\n" " kind = 'canine' # class variable shared by all instances\n" "\n" " def __init__(self, name):\n" " self.name = name # instance variable unique to each instance\n" "\n" ">>> d = Dog('Fido')\n" ">>> e = Dog('Buddy')\n" ">>> d.kind # shared by all dogs\n" "'canine'\n" ">>> e.kind # shared by all dogs\n" "'canine'\n" ">>> d.name # unique to d\n" "'Fido'\n" ">>> e.name # unique to e\n" "'Buddy'" msgstr "" "class Dog:\n" "\n" " kind = 'canine' # 全インスタンスで共有されるクラス変数\n" "\n" " def __init__(self, name):\n" " self.name = name # インスタンスごとに固有のインスタンス変数\n" "\n" ">>> d = Dog('Fido')\n" ">>> e = Dog('Buddy')\n" ">>> d.kind # すべての犬で共有\n" "'canine'\n" ">>> e.kind # すべての犬で共有\n" "'canine'\n" ">>> d.name # d 固有\n" "'Fido'\n" ">>> e.name # e 固有\n" "'Buddy'" #: ../../tutorial/classes.rst:422 msgid "" "As discussed in :ref:`tut-object`, shared data can have possibly surprising " "effects involving :term:`mutable` objects such as lists and dictionaries. " "For example, the *tricks* list in the following code should not be used as a " "class variable because just a single list would be shared by all *Dog* " "instances::" msgstr "" ":ref:`tut-object` で述べたように、共有データはリストや辞書のよう" "な :term:`mutable` オブジェクトで予期せぬ作用を生じることがあります。例えば、" "以下のコードの *tricks* リストはクラス変数として使われるべきではありません、" "なぜなら単一のリストがすべての *Dog* インスタンスで共有されてしまうからです::" #: ../../tutorial/classes.rst:428 msgid "" "class Dog:\n" "\n" " tricks = [] # mistaken use of a class variable\n" "\n" " def __init__(self, name):\n" " self.name = name\n" "\n" " def add_trick(self, trick):\n" " self.tricks.append(trick)\n" "\n" ">>> d = Dog('Fido')\n" ">>> e = Dog('Buddy')\n" ">>> d.add_trick('roll over')\n" ">>> e.add_trick('play dead')\n" ">>> d.tricks # unexpectedly shared by all dogs\n" "['roll over', 'play dead']" msgstr "" "class Dog:\n" "\n" " tricks = [] # クラス変数の間違った使用\n" "\n" " def __init__(self, name):\n" " self.name = name\n" "\n" " def add_trick(self, trick):\n" " self.tricks.append(trick)\n" "\n" ">>> d = Dog('Fido')\n" ">>> e = Dog('Buddy')\n" ">>> d.add_trick('roll over')\n" ">>> e.add_trick('play dead')\n" ">>> d.tricks # 意図せず すべての犬で共有\n" "['roll over', 'play dead']" #: ../../tutorial/classes.rst:445 msgid "Correct design of the class should use an instance variable instead::" msgstr "このクラスの正しい設計ではインスタンス変数を代わりに使用するべきです::" #: ../../tutorial/classes.rst:447 msgid "" "class Dog:\n" "\n" " def __init__(self, name):\n" " self.name = name\n" " self.tricks = [] # creates a new empty list for each dog\n" "\n" " def add_trick(self, trick):\n" " self.tricks.append(trick)\n" "\n" ">>> d = Dog('Fido')\n" ">>> e = Dog('Buddy')\n" ">>> d.add_trick('roll over')\n" ">>> e.add_trick('play dead')\n" ">>> d.tricks\n" "['roll over']\n" ">>> e.tricks\n" "['play dead']" msgstr "" "class Dog:\n" "\n" " def __init__(self, name):\n" " self.name = name\n" " self.tricks = [] # 犬ごとに新しい空リストを作る\n" "\n" " def add_trick(self, trick):\n" " self.tricks.append(trick)\n" "\n" |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |