Python 実行環境の補足
コマンドラインから python.exe を実行するとインタラクティブモードで起動する。
>>> というプロンプトから直接 Python の文を入力して結果を表示できる。
複数行にわたると python.exe が解釈した場合、二行目以降ではプロンプトが ... に変わる。
インタラクティブモードを終了する場合は >>> のプロンプトで Ctrl+Z を入力して Enter.
C:\python>python Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> print 0 0 >>> print """test ... string""" test string >>> ^Z C:\python>
Python でクラスや関数の使い方が分からない場合は属性*1を表示する dir が使える。 dir は変数にも使える。
関数の使い方を知りたい場合には help が使える。
>>> f=open("sample.txt") >>> dir(f) ['__class__', '__delattr__', '__doc__', '__enter__', '__exit__', '__getattribute__', '__hash__', '__init__', '__iter__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', 'close', 'closed', 'encoding', 'fileno', 'flush', 'isatty', 'mode', 'name', 'newlines', 'next', 'read', 'readinto', 'readline', 'readlines', 'seek', 'softspace', 'tell', 'truncate', 'write', 'writelines', 'xreadlines'] >>> help(f.write) Help on built-in function write: write(...) write(str) -> None. Write string str to file. Note that due to buffering, flush() or close() may be needed before the file on disk reflects the data written. >>>