1. 程式人生 > >python學習筆記三 模組

python學習筆記三 模組

最簡單建立模組的方法就是建立以.py結尾的副檔名的檔案,在檔案中包含函式和變數。

例一:helloworld模組

模組原型:

#helloworld.py<pre name="code" class="python"><pre name="code" class="python" style="font-size:10px;"><span style="font-size:10px;">def hello():
    print("hello world!")
__version__ = '0.1'</span>

模組使用效果:

<pre name="code" class="python" style="font-size:10px;">#test.py
<span style="font-family:Arial, Helvetica, sans-serif;">import  helloworld</span>
helloword.hello()
print("version",helloworld.__version__)

注意:helloworld.py必須和test.py在同一個資料夾

輸出結果:

hello world!
('version', '0.1')