開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc.(Text Editor)
- プログラミング言語: Python
『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のV部(モジュール)の20章(インポート、リロード)2.を解いてみる。
その他参考書籍
2.(fromステートメントとimportステートメント)
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3 #-*- coding: utf-8 -*- a = 10 def f(): print("Hello, Python!")
入出力結果(Terminal)
$ python Python 3.3.0 (default, Sep 29 2012, 08:16:08) [GCC 4.2.1 Compatible Apple Clang 3.1 (tags/Apple/clang-318.0.58)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import sample # importでは、完全名が必要 >>> sample.a 10 >>> sample.f() Hello, Python! >>> a Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'a' is not defined >>> f() Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'f' is not defined >>> quit() $ python Python 3.3.0 (default, Sep 29 2012, 08:16:08) [GCC 4.2.1 Compatible Apple Clang 3.1 (tags/Apple/clang-318.0.58)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> from sample import f # fromでは、属性をコピー(属性名だけでいい) >>> sample.f() Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'sample' is not defined >>> f() Hello, Python! >>> a Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'a' is not defined >>> sample.a Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'sample' is not defined >>> from sample import * >>> a 10 >>> f() Hello, Python! >>> sample.a Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'sample' is not defined >>> quit() $
0 コメント:
コメントを投稿