1. 程式人生 > >【PYTHON】模組(module)使用

【PYTHON】模組(module)使用

Python module

A module is a collection of functions and variables that have been bundled together in a single file.

working with the functions defined in modules

匯入: import my_module 使用: my_module.some_function() 當模組名字太長時,匯入: import my_module as m 使用: m.function1() 當模組包含的function太多,而只需使用其中幾個時: from my_module import function1 使用:
function1() 缺點是匯入的這個function前不加模組字首後,能被重寫 匯入所有function和object: from my_module import * 使用: function1() 缺點是會汙染global namespace

內部執行如下:

working with the variables defined in modules

匯入: import math 使用: print(math.pi) 模組中variables的使用類似functions