さらに修正.
@@ -76,52 +76,35 @@ | ||
76 | 76 | 引数の受け渡し |
77 | 77 | -------------- |
78 | 78 | |
79 | -スクリプト名と引数を指定してインタプリタを起動した場合、スクリプト名やスクリプト名以後に指定した引数は、スクリプトからは ``sys.argv`` で | |
80 | -アクセスできるようになります。 ``sys.argv`` は文字列からなるリストで、少なくとも一つ要素が入っています。スクリプト名も引数も指定し | |
81 | -なければ ``sys.argv[0]`` は空の文字列になります。スクリプト名の代わりに ``'-'`` (標準入力を意味します) を指定すると、 | |
82 | -``sys.argv[0]`` は ``'-'`` になります。 :option:`-c` *command* を使うと、 ``sys.argv[0]`` は | |
83 | -``'-c'`` になります。 :option:`-m` *module* を使った場合、 ``sys.argv[0]`` は | |
84 | -モジュールのフルパスになります。 :option:`-c` *command* や :option:`-m` *module* | |
85 | -の後ろにオプションを指定した場合、Python インタプリタ自体はこれらの引数を処理せず、 ``sys.argv`` を介して | |
79 | +スクリプト名と引数を指定してインタプリタを起動した場合、スクリプト名や | |
80 | +スクリプト名以後に指定した引数は、スクリプトからは ``sys.argv`` で | |
81 | +アクセスできるようになります。 | |
82 | +``sys.argv`` は文字列からなるリストで、少なくとも一つ要素が入っています。 | |
83 | +スクリプト名も引数も指定しなければ ``sys.argv[0]`` は空の文字列になります。 | |
84 | +スクリプト名の代わりに ``'-'`` (標準入力を意味します) を指定すると、 | |
85 | +``sys.argv[0]`` は ``'-'`` になります。 :option:`-c` *command* を使うと、 | |
86 | +``sys.argv[0]`` は ``'-c'`` になります。 :option:`-m` *module* を使った場合、 | |
87 | +``sys.argv[0]`` はモジュールのフルパスになります。 | |
88 | +:option:`-c` *command* や :option:`-m` *module* の後ろにオプションを指定した場合、 | |
89 | +Python インタプリタ自体はこれらの引数を処理せず、 ``sys.argv`` を介して | |
86 | 90 | *command* や *module* から扱えるようになります。 |
87 | 91 | |
88 | -.. % Argument Passing | |
89 | -.. % % When known to the interpreter, the script name and additional | |
90 | -.. % % arguments thereafter are passed to the script in the variable | |
91 | -.. % % \code{sys.argv}, which is a list of strings. Its length is at least | |
92 | -.. % % one; when no script and no arguments are given, \code{sys.argv[0]} is | |
93 | -.. % % an empty string. When the script name is given as \code{'-'} (meaning | |
94 | -.. % % standard input), \code{sys.argv[0]} is set to \code{'-'}. When | |
95 | -.. % % \programopt{-c} \var{command} is used, \code{sys.argv[0]} is set to | |
96 | -.. % \code{'-c'}. When \programopt{-m} \var{module} is used, \code{sys.argv[0]} | |
97 | -.. % is set to the full name of the located module. Options found after | |
98 | -.. % \programopt{-c} \var{command} or \programopt{-m} \var{module} are | |
99 | -.. % not consumed | |
100 | -.. % by the Python interpreter's option processing but left in \code{sys.argv} for | |
101 | -.. % the command or module to handle. | |
102 | 92 | |
103 | - | |
104 | 93 | .. _tut-interactive: |
105 | 94 | |
106 | 95 | 対話モード |
107 | 96 | ---------- |
108 | 97 | |
109 | -インタプリタが命令を端末 (tty) やコマンドプロンプトから読み取っている場合、インタプリタは *対話モード (interactive mode)* | |
110 | -で動作しているといいます。このモードでは、インタプリタは *一次プロンプト (primary prompt)* | |
111 | -を表示して、ユーザにコマンドを入力するようします。一次プロンプトは普通、三つの「大なり記号」 (``>>>``) です。一つの入力が次の行まで続く | |
112 | -(行継続: continuation line を行う) 場合、インタプリタは *二次プロンプト (secondary prompt)* を表示します。二 | |
113 | -次プロンプトは、デフォルトでは三つのドット (``...``) です。インタプリタは、最初のプロンプトを出す前にバージョン番号と著作権表示から始ま | |
114 | -る起動メッセージを出力します。 | |
98 | +インタプリタが命令を端末 (tty) やコマンドプロンプトから読み取っている場合、 | |
99 | +インタプリタは *対話モード (interactive mode)* で動作しているといいます。 | |
100 | +このモードでは、インタプリタは *一次プロンプト (primary prompt)* を表示して、 | |
101 | +ユーザにコマンドを入力するよう促します。 | |
102 | +一次プロンプトは普通、三つの「大なり記号」 (``>>>``) です。一つの入力が次の行まで続く | |
103 | +(行継続: continuation line を行う) 場合、インタプリタは | |
104 | +*二次プロンプト (secondary prompt)* を表示します。二次プロンプトは、 | |
105 | +デフォルトでは三つのドット (``...``) です。 | |
106 | +インタプリタは、最初のプロンプトを出す前にバージョン番号と著作権表示から始まる起動メッセージを出力します。 | |
115 | 107 | |
116 | -.. % Interactive Mode | |
117 | -.. % % When commands are read from a tty, the interpreter is said to be in | |
118 | -.. % % \emph{interactive mode}. In this mode it prompts for the next command | |
119 | -.. % % with the \emph{primary prompt}, usually three greater-than signs | |
120 | -.. % % (\samp{>\code{>}>~}); for continuation lines it prompts with the | |
121 | -.. % % \emph{secondary prompt}, by default three dots (\samp{...~}). | |
122 | -.. % % The interpreter prints a welcome message stating its version number | |
123 | -.. % % and a copyright notice before printing the first prompt: | |
124 | - | |
125 | 108 | :: |
126 | 109 | |
127 | 110 | python |
@@ -129,11 +112,8 @@ | ||
129 | 112 | Type "help", "copyright", "credits" or "license" for more information. |
130 | 113 | >>> |
131 | 114 | |
132 | -行継続は、例えば以下の :keyword:`if` 文のように、複数の行からなる構文を入力するときに必要です: | |
115 | +行継続は、例えば以下の :keyword:`if` 文のように、複数の行からなる構文を入力するときに必要です。 | |
133 | 116 | |
134 | -.. % % Continuation lines are needed when entering a multi-line construct. | |
135 | -.. % % As an example, take a look at this \keyword{if} statement: | |
136 | - | |
137 | 117 | :: |
138 | 118 | |
139 | 119 | >>> the_world_is_flat = 1 |
@@ -142,93 +122,56 @@ | ||
142 | 122 | ... |
143 | 123 | Be careful not to fall off! |
144 | 124 | |
145 | -.. % %% XXX ProofReeding Done To Here XXX %%% | |
146 | 125 | |
147 | - | |
148 | 126 | .. _tut-interp: |
149 | 127 | |
150 | 128 | インタプリタとその環境 |
151 | 129 | ====================== |
152 | 130 | |
153 | -.. % The Interpreter and Its Environment | |
154 | - | |
155 | - | |
156 | 131 | .. _tut-error: |
157 | 132 | |
158 | 133 | エラー処理 |
159 | 134 | ---------- |
160 | 135 | |
161 | -エラーが発生すると、インタプリタはエラーメッセージとスタックトレース (stack trace) を出力します。対話モードにいるときは、インタプリタは | |
162 | -一次プロンプトに戻ります; 入力がファイルからきているときには、インタプリタはスタックトレースを出力した後、非ゼロの終了状態で終了します。 (``try`` | |
163 | -文の :keyword:`except` 節で処理された例外は、ここでいうエラーにはあたりません。) | |
164 | -いくつかのエラーは無条件に致命的であり、非ゼロの終了状態となるプログラムの終了を引き起こします; これにはインタプリタ内部の矛盾や | |
165 | -ある種のメモリ枯渇が当てはまります。エラーメッセージは全て標準エラー出力ストリームに書き込まれます; | |
136 | +エラーが発生すると、インタプリタはエラーメッセージとスタックトレース | |
137 | +(stack trace) を出力します。対話モードにいるときは、インタプリタは | |
138 | +一次プロンプトに戻ります; | |
139 | +入力がファイルからきているときには、インタプリタはスタックトレースを出力した後、 | |
140 | +非ゼロの終了状態で終了します。 (:keyword:`try` 文の :keyword:`except` | |
141 | +節で処理された例外は、ここでいうエラーにはあたりません。) | |
142 | +いくつかのエラーは無条件に致命的であり、非ゼロの終了状態となるプログラムの終了を引き起こします; | |
143 | +これにはインタプリタ内部の矛盾やある種のメモリ枯渇が当てはまります。 | |
144 | +エラーメッセージは全て標準エラー出力ストリームに書き込まれます; | |
166 | 145 | これに対して、実行した命令からの通常出力される内容は標準出力に書き込まれます。 |
167 | 146 | |
168 | -.. % Error Handling | |
169 | -.. % % When an error occurs, the interpreter prints an error | |
170 | -.. % % message and a stack trace. In interactive mode, it then returns to | |
171 | -.. % % the primary prompt; when input came from a file, it exits with a | |
172 | -.. % % nonzero exit status after printing | |
173 | -.. % % the stack trace. (Exceptions handled by an \code{except} clause in a | |
174 | -.. % % \code{try} statement are not errors in this context.) Some errors are | |
175 | -.. % % unconditionally fatal and cause an exit with a nonzero exit; this | |
176 | -.. % % applies to internal inconsistencies and some cases of running out of | |
177 | -.. % % memory. All error messages are written to the standard error stream; | |
178 | -.. % % normal output from the executed commands is written to standard | |
179 | -.. % % output. | |
180 | - | |
181 | 147 | 割り込み文字 (interrupt character、普通は Control-C か DEL) を |
182 | -一次または二次プロンプトに対して打鍵すると、入力が取り消されて一次プロンプトに戻ります。 [#]_ コマンドの実行中に割り込み文字を打鍵すると | |
183 | -:exc:`KeyboardInterrupt` 例外が送出されます。この例外は ``try`` 文で処理できます。 | |
148 | +一次または二次プロンプトに対して打鍵すると、入力が取り消されて一次プロンプトに戻ります。 [#]_ | |
149 | +コマンドの実行中に割り込み文字を打鍵すると | |
150 | +:exc:`KeyboardInterrupt` 例外が送出されます。この例外は :keyword:`try` 文で処理できます。 | |
184 | 151 | |
185 | -.. % % Typing the interrupt character (usually Control-C or DEL) to the | |
186 | -.. % % primary or secondary prompt cancels the input and returns to the | |
187 | -.. % % primary prompt.\footnote{ | |
188 | -.. % % A problem with the GNU Readline package may prevent this. | |
189 | -.. % % } | |
190 | -.. % % Typing an interrupt while a command is executing raises the | |
191 | -.. % % \code{KeyboardInterrupt} exception, which may be handled by a | |
192 | -.. % % \code{try} statement. | |
193 | 152 | |
194 | - | |
195 | 153 | .. _tut-scripts: |
196 | 154 | |
197 | 155 | 実行可能な Python スクリプト |
198 | 156 | ---------------------------- |
199 | 157 | |
200 | -BSD 風の Unix システムでは、Python スクリプトはシェルスクリプトのようにして直接実行可能にできます。これを行うには、以下の行 | |
158 | +BSD 風の Unix システムでは、Python スクリプトはシェルスクリプトのように | |
159 | +直接実行可能にできます。これを行うには、以下の行 | |
201 | 160 | |
202 | -.. % Executable Python Scripts | |
203 | -.. % % On BSD'ish \UNIX{} systems, Python scripts can be made directly | |
204 | -.. % % executable, like shell scripts, by putting the line | |
205 | - | |
206 | 161 | :: |
207 | 162 | |
208 | 163 | #! /usr/bin/env python |
209 | 164 | |
210 | -(ここではインタプリタがユーザの :envvar:`PATH` 上にあると仮定しています) をスクリプトの先頭に置き、スクリプトファイルに実行可能モードを | |
211 | -与えます。 ``#!`` はファイルの最初の2文字でなければなりません。プラットフォームによっては、この最初の行を終端する改行文字が | |
165 | +(ここではインタプリタがユーザの :envvar:`PATH` 上にあると仮定しています) | |
166 | +をスクリプトの先頭に置き、スクリプトファイルに実行可能モードを与えます。 | |
167 | +``#!`` はファイルの最初の2文字でなければなりません。 | |
168 | +プラットフォームによっては、この最初の行を終端する改行文字が | |
212 | 169 | Windows 形式 (``'\r\n'``) ではなく、 Unix形式(``'\n'``)でなければならないことがあります。 |
213 | -ハッシュまたはポンド文字、すなわち ``'#'`` は、Python ではコメントを書き始めるために使われているので注意してください。 | |
170 | +ハッシュまたはポンド文字、すなわち ``'#'`` は、Python ではコメントを書き始めるために | |
171 | +使われていることに注意してください。 | |
214 | 172 | |
215 | -.. % % (assuming that the interpreter is on the user's \envvar{PATH}) at the | |
216 | -.. % % beginning of the script and giving the file an executable mode. The | |
217 | -.. % % \samp{\#!} must be the first two characters of the file. On some | |
218 | -.. % % platforms, this first line must end with a \UNIX-style line ending | |
219 | -.. % % (\character{\e n}), not a Mac OS (\character{\e r}) or Windows | |
220 | -.. % % (\character{\e r\e n}) line ending. Note that | |
221 | -.. % % the hash, or pound, character, \character{\#}, is used to start a | |
222 | -.. % % comment in Python. | |
173 | +:program:`chmod` コマンドを使えば、スクリプトに実行モード (または実行権限) を与えることができます。 | |
223 | 174 | |
224 | -:program:`chmod` コマンドを使えば、スクリプトに実行モード (または実行権限) を与えることができます: | |
225 | - | |
226 | -.. % % The script can be given a executable mode, or permission, using the | |
227 | -.. % % \program{chmod} command: | |
228 | -.. % % begin{verbatim} | |
229 | -.. % % $ chmod +x myscript.py | |
230 | -.. % % end{verbatim} % $ <-- bow to font-lock | |
231 | - | |
232 | 175 | :: |
233 | 176 | |
234 | 177 | $ chmod +x myscript.py |
@@ -243,34 +186,40 @@ | ||
243 | 186 | ソースコードの文字コード方式 (encoding) |
244 | 187 | --------------------------------------- |
245 | 188 | |
246 | -ASCII 形式でない文字コード化方式 (エンコーディング: encoding) を Python ソースコードファイル中で使うことができます。最良の方法は、 | |
247 | -``#!`` 行の直後に一行かそれ以上の特殊なコメントを挿入して、ソースファイルのエンコードを指定するというものです: | |
189 | +ASCII 形式でない文字コード化方式 (エンコーディング: encoding) を Python | |
190 | +ソースコードファイル中で使うことができます。 | |
191 | +最良の方法は、 ``#!`` 行の直後に一行かそれ以上の特殊なコメントを挿入して、 | |
192 | +ソースファイルのエンコードを指定するというものです。 | |
248 | 193 | |
249 | -.. % % It is possible to use encodings different than \ASCII{} in Python source | |
250 | -.. % % files. The best way to do it is to put one more special comment line | |
251 | -.. % % right after the \code{\#!} line to define the source file encoding: | |
252 | - | |
253 | 194 | :: |
254 | 195 | |
255 | - # -\*- coding: encoding -\*- | |
196 | + # -*- coding: エンコーディング -*- | |
256 | 197 | |
257 | - | |
258 | -このように宣言しておくと、ソースファイル中の全ての文字は *encoding* という文字コードでエンコードされているものとして扱われ、Unicode 文字列 | |
198 | +このように宣言しておくと、ソースファイル中の全ての文字は *エンコーディング* | |
199 | +という文字コードでエンコードされているものとして扱われ、Unicode 文字列 | |
259 | 200 | リテラルを指定したエンコードで直接記述できます。 |
260 | -利用可能なエンコードのリストは Python ライブラリリファレンスの | |
201 | +実際に利用できるエンコードのリストは Python ライブラリリファレンスの | |
261 | 202 | :mod:`codecs` の節にあります。 |
262 | 203 | |
263 | -.. % % With that declaration, all characters in the source file will be treated as | |
264 | -.. % % {}\code{iso-8859-1}, and it will be | |
265 | -.. % % possible to directly write Unicode string literals in the selected | |
266 | -.. % % encoding. The list of possible encodings can be found in the | |
267 | -.. % % \citetitle[../lib/lib.html]{Python Library Reference}, in the section | |
268 | -.. % % on \ulink{\module{codecs}}{../lib/module-codecs.html}. | |
204 | +.. note:: | |
205 | + 訳注: *エンコーディング* の部分には、 | |
206 | + 実際には ``utf-8`` や ``cp932`` など、そのソースのエンコーディングを記述します。 | |
207 | + プログラムから日本語を扱う場合には、必ずある程度の文字コードの知識が必要になります。 | |
208 | + もし文字コードについてよく判らないのであれば、まずは英語だけ扱いながらチュートリアルを読み進めて、 | |
209 | + 並行して文字コードについても勉強してみましょう。 | |
269 | 210 | |
270 | -例えばユーロ通過記号を含む Unicode リテラルを書くには、 ISO-8859-15 エンコーディングを使えます。 ISO-8859-15 では、ユーロ | |
271 | -通過記号の序数 (ordinal) は 164 です。以下のスクリプトは 8364 という値 (Unicode でユーロ記号に対応するコードポイントの値) を | |
272 | -出力して終了します: | |
211 | + 現在は、 Unicode の扱い易さの観点から、推奨される文字コードは ``utf-8`` です。 | |
212 | + ただし、 ``utf-8`` でソースを書いた場合、 ``utf-8`` を表示できない Windows の | |
213 | + コマンドプロンプトは ``print "こんにちは"`` を実行すると文字化けを起こすでしょう。 | |
214 | + その場合は、互換性のために古い ``cp932`` エンコーディングを使うか、 | |
215 | + ``print u"こんにちは"`` のように Unicode 文字列を使います。 | |
273 | 216 | |
217 | +例えばユーロ通貨記号を含む Unicode リテラルを書くには、 ISO-8859-15 | |
218 | +エンコーディングを使えます。 | |
219 | +ISO-8859-15 では、ユーロ通過記号の序数 (ordinal) は 164 です。 | |
220 | +以下のスクリプトは 8364 という値 (Unicode でユーロ記号に対応するコードポイントの値) | |
221 | +を出力して終了します。 | |
222 | + | |
274 | 223 | .. % For example, to write Unicode literals including the Euro currency |
275 | 224 | .. % symbol, the ISO-8859-15 encoding can be used, with the Euro symbol |
276 | 225 | .. % having the ordinal value 164. This script will print the value 8364 |
@@ -298,10 +247,12 @@ | ||
298 | 247 | .. % % and earlier), and also not understood by the operating system for |
299 | 248 | .. % +script files with \code{\#!} lines (only used on \UNIX{} systems). |
300 | 249 | |
301 | -UTF-8 を (シグネチャやエンコード宣言を行って) 使うと、世界中のほとんどの言語で使われている文字を文字列リテラルやコメントの中に | |
302 | -同時に使うことができます。識別子に対する非 ASCII 文字の使用はサポートされていません。全ての文字を正しく表示できるようにするには、使っている | |
303 | -エディタがファイルを UTF-8 であると認識することができなければならず、かつファイル内で使われている全ての文字をサポートするようなフォントを | |
304 | -使わなければなりません。 | |
250 | +UTF-8 を (シグネチャやエンコード宣言を行って) 使うと、世界中のほとんどの | |
251 | +言語で使われている文字を文字列リテラルやコメントの中に同時に使うことができます。 | |
252 | +識別子に対する非 ASCII 文字の使用はサポートされていません。 | |
253 | +全ての文字を正しく表示できるようにするには、使っているエディタがファイルを | |
254 | +UTF-8 であると認識することができなければならず、かつファイル内で使われている | |
255 | +全ての文字をサポートするようなフォントを使わなければなりません。 | |
305 | 256 | |
306 | 257 | .. % % By using UTF-8 (either through the signature or an encoding |
307 | 258 | .. % % declaration), characters of most languages in the world can be used |