2. 파이썬 인터프리터 사용하기
*****************************


2.1. 인터프리터 실행하기
========================

The Python interpreter is usually installed as "/usr/local/bin/python"
on those machines where it is available; putting "/usr/local/bin" in
your Unix shell's search path makes it possible to start it by typing
the command

   python

to the shell.  Since the choice of the directory where the interpreter
lives is an installation option, other places are possible; check with
your local Python guru or system administrator.  (E.g.,
"/usr/local/python" is a popular alternative location.)

On Windows machines, the Python installation is usually placed in
"C:\Python27", though you can change this when you're running the
installer.  To add this directory to your path,  you can type the
following command into the command prompt in a DOS box:

   set path=%path%;C:\python27

기본 프롬프트에서 EOF(end-of-file) 문자(유닉스에서는 "Control-D", 윈도
우에서는 "Control-Z")를 입력하면 인터프리터가 종료하고, 종료 상태 코드
는 0 이 됩니다. 이 방법이 통하지 않는다면 "quit()" 명령을 입력해서 인
터프리터를 종료시킬 수 있습니다.

The interpreter's line-editing features usually aren't very
sophisticated.  On Unix, whoever installed the interpreter may have
enabled support for the GNU readline library, which adds more
elaborate interactive editing and history features. Perhaps the
quickest check to see whether command line editing is supported is
typing "Control-P" to the first Python prompt you get.  If it beeps,
you have command line editing; see Appendix 대화형 입력 편집 및 히스토
리 치환 for an introduction to the keys.  If nothing appears to
happen, or if "^P" is echoed, command line editing isn't available;
you'll only be able to use backspace to remove characters from the
current line.

인터프리터는 어느 정도 유닉스 셸처럼 동작합니다: tty 장치에 표준 입력
이 연결된 상태로 실행되면, 대화형으로 명령을 읽고 실행합니다; 파일명을
인자로 주거나 파일을 표준입력으로 연결한 상태로 실행되면 스크립트를 읽
고 실행합니다.

인터프리터를 실행하는 두 번째 방법은 "python -c command [arg] ..." 인
데, *command* 에 있는 문장들을 실행합니다. 셸의 "-c" 옵션에 해당합니다
. 파이썬 문장은 종종 셸에서 특별한 의미가 있는 공백이나 다른 문자들을
포함하기 때문에, *command* 전체를 작은따옴표로 감싸주는 것이 좋습니다.

몇몇 파이썬 모듈들은 스크립트로도 쓸모가 있습니다. "python -m module
[arg] ..." 로 실행할 수 있는데, 마치 *module* 모듈 소스 파일의 경로명
을 명령행에 입력한 것처럼 실행되게 됩니다.

스크립트 파일이 사용될 때, 때로 스크립트를 실행한 후에 대화형 모드로
들어가는 것이 편리할 때가 있습니다. 스크립트 앞에 "-i" 를 전달하면 됩
니다.

All command-line options are described in Command line and
environment.


2.1.1. 인자 전달
----------------

스크립트 이름과 추가의 인자들이 인터프리터로 전달될 때, 문자열의 목록
으로 변환된 후 "sys" 모듈의 "argv" 변수에 저장됩니다. "import sys" 를
사용해서 이 목록에 접근할 수 있습니다. 목록의 길이는 최소한 1이고, 스
크립트도 추가의 인자도 없는 경우로, "sys.argv[0]" 은 빈 문자열입니다.
스크립트 이름을 "'-'" (표준 입력을 뜻한다) 로 주면 "sys.argv[0]" 는
"'-'" 가 됩니다. "-c" *command* 가 사용되면 "sys.argv[0]" 는 "'-c'" 로
설정됩니다. "-m" *module* 이 사용되면 "sys.argv[0]" 는 모듈의 절대 경
로명이 됩니다. "-c" *command* 나 "-m" *module* 뒤에 오는 옵션들은 파이
썬 인터프리터가 소모하지 않고 명령이나 모듈이 처리하도록 "sys.argv" 로
전달됩니다.


2.1.2. 대화형 모드
------------------

명령을 tty 에서 읽을 때, 인터프리터가 *대화형 모드* 로 동작한다고 말합
니다. 이 모드에서는 *기본 프롬프트* 를 표시해서 다음 명령을 요청하는데
, 보통 세 개의 ...보다 크다 기호입니다 (">>>"); 한 줄로 끝나지 않고 이
어지는 줄의 입력을 요청할 때는 보조 프롬프트가 사용되는데, 기본적으로
세 개의 점입니다 ("..."). 인터프리터는 첫 번째 프롬프트를 인쇄하기 전
에 버전 번호와 저작권 공지를 포함하는 환영 메시지를 출력합니다.

   python
   Python 2.7 (#1, Feb 28 2010, 00:02:06)
   Type "help", "copyright", "credits" or "license" for more information.
   >>>

이어지는 줄은 여러 줄로 구성된 구조물을 입력할 때 필요합니다. 예를 들
자면, 이런 식의 "if" 문이 가능합니다:

   >>> the_world_is_flat = 1
   >>> if the_world_is_flat:
   ...     print "Be careful not to fall off!"
   ...
   Be careful not to fall off!

대화형 모드에 대해 더 알고 싶다면, 대화형 모드 를 보세요.


2.2. 인터프리터와 환경
======================


2.2.1. 소스 코드 인코딩
-----------------------

By default, Python source files are treated as encoded in ASCII. To
declare an encoding other than the default one, a special comment line
should be added as the *first* line of the file.  The syntax is as
follows:

   # -*- coding: encoding -*-

*encoding* 은 파이썬이 지원하는 코덱 ("codecs") 중 하나여야 합니다.

예를 들어, Windows-1252 인코딩을 사용하도록 선언하려면, 소스 코드 파일
의 첫 줄은 이렇게 되어야 합니다:

   # -*- coding: cp1252 -*-

첫 줄 규칙의 한가지 예외는 소스 코드가 유닉스 "셔뱅 (shebang)" 줄 로
시작하는 경우입니다. 이 경우에, 인코딩 선언은 두 번째 줄에 들어갑니다.
예를 들어:

   #!/usr/bin/env python
   # -*- coding: cp1252 -*-
