2012年8月3日金曜日

開発環境

『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のV部(モジュール)の21章(モジュールに関する高度なテクニックとインポート)練習問題4を解いてみる。

4.

コード(TextWrangler)

sample.py

#!/usr/bin/env python
#encoding: utf-8

a = 10
b = "python"
def f():
 print("Hello, Python!")
def g():
 print("Hello, World!")

入出力結果(Terminal)

$ 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.
>>> name = "sample"
>>> exec("import " + name)
>>> sample.a
10
>>> sample.b
'python'
>>> sample.f()
Hello, Python!
>>> sample.g()
Hello, World!
>>> sample
<module 'sample' from 'sample.py'>
>>> m = __import__(name)
>>> m.a
10
>>> m.b
'python'
>>> m.f()
Hello, Python!
>>> m.g()
Hello, World!
>>> m
<module 'sample' from 'sample.py'>
>>> quit()
$

0 コメント:

コメントを投稿