HiiHahWIKI - making some notes for... -
E71:Pythonのパッケージを使ってみる
をテンプレートにして作成
開始行:
*E71:Pythonのパッケージを使ってみる [#q89e52dd]
個人的開発中のソースが大きくなってきたので、Pythonのパッ...
さすがに、たいした規模じゃなくても、E71で100行を超えるソ...
***Pythonのパッケージ [#x9b6ad9e]
Pythonでは、モジュールを格納するディレクトリに「__init__....
「__init__.py」は、そのパッケージ内のモジュールがインポー...
「__init__.py」の中身は、何もなければ空でいいみたいです。...
詳しくはリファレンスマニュアルのチュートリアル6.4を参照さ...
-http://www.python.jp/doc/2.4/tut/node8.html
***実際に使ってみる [#xdeb266a]
今回の例では、以下のようなパッケージ構成を作ります。
┣packageuse1.py パッケージを利用するためのスクリプト
┗testpackage : パッケージディレクトリ
┣__init__.py : パッケージの初期化スクリプト
┣test1.py : パッケージに含むモジュール
┗test2.py : パッケージに含むモジュール
「testpackage」というパッケージの中に、「test1」、「test2...
test0.py、__init__.py、test1.py、test2.pyは以下のとおり
■packageuse1.py
from testpackage import *
print dir()
if __name__ == '__main__':
test1.TestClass1().printtest()
■__init__.py
print '__init__.py was executed'
__all__ = ['test1']
■test1.py
class TestClass1:
def __init__(self):
print 'TestClass1 Created!'
def printtest(self):
print 'TestClass1 method was executed'
■test2.py
class TestClass2:
def __init__(self):
print 'TestClass2 Created!'
def printtest(self):
print 'TestClass2 method was executed'
まあ、内容は造作もないですな~
この構成で、test0.pyを実行すると、以下のような結果が得ら...
__init__.py was executed
['__builtins__', '__doc__', '__file__', '__name__', '__p...
TestClass1 Created!
TestClass1 method was executed
実行結果より、
+パッケージの中のモジュールを使う前に、__init__.pyの中が...
+「__init__.py」の「__ALL__」を設定したことで、「import *...
ってことがわかったりします。
自分が知りたかったことは以上!
***リンク [#z3948d8c]
-http://www.python.jp/doc/2.4/tut/node8.html
-http://python.matrix.jp/tips/import.html#id15
終了行:
*E71:Pythonのパッケージを使ってみる [#q89e52dd]
個人的開発中のソースが大きくなってきたので、Pythonのパッ...
さすがに、たいした規模じゃなくても、E71で100行を超えるソ...
***Pythonのパッケージ [#x9b6ad9e]
Pythonでは、モジュールを格納するディレクトリに「__init__....
「__init__.py」は、そのパッケージ内のモジュールがインポー...
「__init__.py」の中身は、何もなければ空でいいみたいです。...
詳しくはリファレンスマニュアルのチュートリアル6.4を参照さ...
-http://www.python.jp/doc/2.4/tut/node8.html
***実際に使ってみる [#xdeb266a]
今回の例では、以下のようなパッケージ構成を作ります。
┣packageuse1.py パッケージを利用するためのスクリプト
┗testpackage : パッケージディレクトリ
┣__init__.py : パッケージの初期化スクリプト
┣test1.py : パッケージに含むモジュール
┗test2.py : パッケージに含むモジュール
「testpackage」というパッケージの中に、「test1」、「test2...
test0.py、__init__.py、test1.py、test2.pyは以下のとおり
■packageuse1.py
from testpackage import *
print dir()
if __name__ == '__main__':
test1.TestClass1().printtest()
■__init__.py
print '__init__.py was executed'
__all__ = ['test1']
■test1.py
class TestClass1:
def __init__(self):
print 'TestClass1 Created!'
def printtest(self):
print 'TestClass1 method was executed'
■test2.py
class TestClass2:
def __init__(self):
print 'TestClass2 Created!'
def printtest(self):
print 'TestClass2 method was executed'
まあ、内容は造作もないですな~
この構成で、test0.pyを実行すると、以下のような結果が得ら...
__init__.py was executed
['__builtins__', '__doc__', '__file__', '__name__', '__p...
TestClass1 Created!
TestClass1 method was executed
実行結果より、
+パッケージの中のモジュールを使う前に、__init__.pyの中が...
+「__init__.py」の「__ALL__」を設定したことで、「import *...
ってことがわかったりします。
自分が知りたかったことは以上!
***リンク [#z3948d8c]
-http://www.python.jp/doc/2.4/tut/node8.html
-http://python.matrix.jp/tips/import.html#id15
ページ名: