開発環境
- OS X Lion - Apple(OS)
- TextWrangler(Text Editor) (BBEditの無料機能制限版、light版)
- Script言語: Python
『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7)のIV部(モジュール)のまとめ演習3(__name__属性)を解いてみる。
3.
コード(TextWrangler)
#!/usr/bin/env python
#encoding: utf-8
def countLines(name):
return len(open(name).readlines())
def countChars(name):
return len(open(name).read())
def test(name):
return countLines(name), countChars(name)
if __name__ == '__main__':
print test('mymod.py')
入出力結果(Terminal)
$ ./mymod.py (14, 263) $ python Python 2.6.6 (r266:84374, Aug 31 2010, 11:00:51) [GCC 4.0.1 (Apple Inc. build 5493)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import mymod >>> name='mymod.py' >>> mymod.countLines(name) 14 >>> mymod.countChars(name) 263 >>> mymod.test(name) (14, 263) >>> print mymod.__name__ mymod >>> __name__ '__main__' $
0 コメント:
コメントを投稿