2012年2月11日土曜日

開発環境

『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7)のIV部(モジュール)のまとめ演習2(fromステートメント)を解いてみる。

2.

コード(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)

入出力結果(Terminal)

$ 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.
>>> from mymod import countLines
>>> name = 'mymod.py'
>>> countLines(name)
11
>>> from mymod import countChars
>>> countChars(name)
211
>>> from mymod import *
>>> test(name)
(11, 211)
>>> 
$

0 コメント:

コメントを投稿