開発環境
- OS X Lion - Apple(OS)
- TextWrangler(Text Editor) (BBEditの無料機能制限版、light版)
- Script言語: Python
『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のV部(モジュール)のまとめ演習3(__name__属性)解いてみる。
3.
コード(TextWrangler)
sample.py
#!/usr/bin/env python #encoding: utf-8 def countLines(name): file = open(name) return len(file.readlines()) def countChars(name): file = open(name) return len(file.read()) def test(name): result = ("行数: " + str(countLines(name)) + ", 文字数: " + str(countChars(name))) return result if __name__ == "__main__": name = "sample.py" for f in [countLines,countChars,test]: print(f(name))
入出力結果(Terminal)
$ ./sample.py 20 398 行数: 20, 文字数: 398 $ python Python 3.2.3 (default, Apr 18 2012, 20:17:30) [GCC 4.2.1 Compatible Apple Clang 3.0 (tags/Apple/clang-211.12)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import sample >>> name = 'sample.py' >>> sample.countLines(name) 20 >>> sample.countChars(name) 398 >>> sample.test(name) '行数: 20, 文字数: 398' >>> from sample import * >>> countLines(name) 20 >>> countChars(name) 398 >>> test(name) '行数: 20, 文字数: 398' >>> quit() $
0 コメント:
コメントを投稿