標籤:uil __file__ 模組名 color loader pack 標準 參考 ann
引入模組
文法
import modeName
one.py
def printSomething(str): print("hello " + str);
main.py
import oneone.printSomething("jiao"); #hello jiaoone.printSomething("fftu"); #hello fftu
部分引入
文法
from modeName import functionName, varName
one.py
def printSomething(str): print("hello " + str);str = "you should go now";def printbye(): print(str);
main.py
from one import printSomething, printbyeprintSomething("jiao"); #hello jiaoprintbye(); #you should go now
注意:部分引入不能使用模組名one
除了函數外,也可以引入模組的變數(不引入訪問報錯,但模組函數中是可以訪問這個變數的)
全部引入(將模組內所有函數、變數都引入當前檔案)
文法
from one import *
引入後用法和部分引入一樣
dir()函數
以一個字串列表的形式返回模組內定義的所有名稱
import oneprint(dir(one));
#[‘__builtins__‘, ‘__cached__‘, ‘__doc__‘, ‘__file__‘, ‘__loader__‘, ‘__name__‘, ‘__package__‘, ‘__spec__‘, ‘printSomething‘, ‘printbye‘, ‘str1‘]
沒有傳參返回當前檔案定義的所有名稱(引入的模組不做解析,只有一個名稱)
import onenumA = 10;strB = "haha";print(dir());#[‘__annotations__‘, ‘__builtins__‘, ‘__cached__‘, ‘__doc__‘, ‘__file__‘, ‘__loader__‘, ‘__name__‘, ‘__package__‘, ‘__spec__‘, ‘numA‘, ‘one‘, ‘strB‘]
這樣會解析引入模組中的對象
from one import printSomething, printbye, str1numA = 10;strB = "haha";print(dir());#[‘__annotations__‘, ‘__builtins__‘, ‘__cached__‘, ‘__doc__‘, ‘__file__‘, ‘__loader__‘, ‘__name__‘, ‘__package__‘, ‘__spec__‘, ‘numA‘, ‘printSomething‘, ‘printbye‘, ‘str1‘, ‘strB‘]
標準模組
Python 本身帶著一些標準的模組庫,在 Python 庫參考文檔中將會介紹到(就是後面的"庫參考文檔")。
有些模組直接被構建在解析器裡,這些雖然不是一些語言內建的功能,但是他卻能很高效的使用,甚至是系統級調用也沒問題。
這些組件會根據不同的作業系統進行不同形式的配置,比如 winreg 這個模組就只會提供給 Windows 系統。
python--模組