2012年7月30日月曜日

開発環境

『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のV部(パッケージインポート)の20章(パッケージインポートとインポート)練習問題4を解いてみる。

4.

同じ名前の属性がある場合。

コード(TextWrangler)

sample.py

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

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

sample1.py

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

a = 20
b = "python3"
def f():
 print("Hello, python3!")

入出力結果(Terminal)

$ ls -R sample_folder
__init__.py sample_folder sample_folder1

sample_folder/sample_folder:
__init__.py sample.py

sample_folder/sample_folder1:
__init__.py sample1.py
$ 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.
>>> from sample_folder.sample_folder.sample import *
>>> a
10
>>> b
'Python'
>>> f()
Hello, Python!
>>> from sample_folder.sample_folder1.sample1 import *
>>> a
20
>>> b
'python3'
>>> f()
Hello, python3!
>>> import sample_folder.sample_folder.sample
>>> import sample_folder.sample_folder1.sample1
>>> sample_folder.sample_folder.sample.a
10
>>> sample_folder.sample_folder.sample.b
'Python'
>>> sample_folder.sample_folder.sample.f()
Hello, Python!
>>> sample_folder.sample_folder1.sample1.a
20
>>> sample_folder.sample_folder1.sample1.b
'python3'
>>> sample_folder.sample_folder1.sample1.f()
Hello, python3!
>>> quit()
$

0 コメント:

コメントを投稿