2012年7月31日火曜日

開発環境

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

1.

from *ステートメントによるコピーの対象から外れる。

コード(TextWrangler)

sample.py

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

a = 10
b = "Python"
_a = 20
_b = "JavaScript"
def f():
 print("Hello, Python!")
def _f():
 print("Hello, JavaScript!")

入出力結果(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.
>>> from sample import *
>>> a
10
>>> b
'Python'
>>> _a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name '_a' is not defined
>>> _b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name '_b' is not defined
>>> f()
Hello, Python!
>>> _f()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name '_f' is not defined
>>> import sample
>>> sample.a
10
>>> sample.b
'Python'
>>> sample._a
20
>>> sample._b
'JavaScript'
>>> sample.f()
Hello, Python!
>>> sample._f()
Hello, JavaScript!
>>> quit()
$

0 コメント:

コメントを投稿