2013年3月16日土曜日

開発環境

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

その他参考書籍

1.

コード(BBEdit)

sample.py

#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-

# モジュールのトップレベルで変数名の先頭に下線を1つ付けると
# from * ステートメントでのインポtー小野際その属性は除外される
# (ただし、完全に隠蔽されるわけではなくimport ステートメントや
# from ステートメントでアスタリスク「*」ではなく変数笑みを指定すれば
# 使える
a = "python"
_a = "PYTHON"
b = "javascript"
_b = "JAVASCRIPT"
def f():
    print("hello, world!")
def _f():
    print("HELLO, WORLD!")

入出力結果(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.
>>> from sample import *
>>> a
'python'
>>> _a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name '_a' is not defined
>>> from sample import _a
>>> _a
'PYTHON'
>>> import sample
>>> sample.b
'javascript'
>>> sample._b
'JAVASCRIPT'
>>> f()
hello, world!
>>> _f()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name '_f' is not defined
>>> sample._f()
HELLO, WORLD!
>>> from sample import _f
>>> _f()
HELLO, WORLD!
>>> quit()
$

0 コメント:

コメントを投稿