您的位置:首頁>正文

我還沒見過哪個人對Python模組居然理解這麼深!實在是太全面了!

然後通過import引入該模組:

一個人自學很有可能因為動力不足而中途放棄, 可以嘗試加入一個或幾個適合自己的網路群體(QQ、 微信、 社區等),尋找志同道合的學習夥伴, 相互交流、相互促進.如果大家想要學習交流歡迎給我私信, 私信關鍵字: 01.02.03.04 都會得到不一樣的資料!系統堅持到關鍵字會自動發送。

1、impor語句

您可以通過在其他一些Python原始檔案執行一個import語句, 來使用任何Python原始檔案作為一個模組。 import 語句的語法如下:

import module1[, module2[,... moduleN] 1

當解釋程式遇到import語句, 如果模組存在於搜索路徑它將導入這個模組。 搜索路徑是一個目錄清單, 解譯器導入模組之前將進行搜索。 例如, 要導入模組 hello.py, 需要在腳本的頂部放置下面的命令 -

這提供了一種簡單的方法來導入模組到當前命名空間中的所有項目; 不過, 需要注意的是在實踐中往往不鼓勵從一個模組或包中使用 * 導入所有, 因為這樣會讓代碼變得很難讀。 不過, 在互動式會話中這樣用很方便省力。

注解

出於性能考慮, 每個模組在每個解譯器會話中只導入一遍。 因此, 如果你修改了你的模組, 需要重啟解譯器;或者, 如果你就是想互動式的測試這麼一個模組, 可以用 imp.reload() 重新載入, 例如import imp; imp.reload(modulename)。

模組中的代碼會被執行, 就像導入它一樣, 不過此時 name 被設置為 “main“。 這相當於, 如果你在模組後加入如下代碼:

if __name__ == "__main__": import sys fib(int(sys.argv[1]))

三、dir() 函數

內置函數 dir() 用於按模組名搜索模組定義,它返回一個字串類型的存儲清單:

結果如下:

['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'fib', 'fib2']['__displayhook__', '__doc__', '__excepthook__', '__interactivehook__', '__loader__', '__name__', '__package__', '__spec__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_debugmallocstats', '_getframe', '_home', '_mercurial', '_xoptions', 'api_version', 'argv', 'base_exec_prefix', 'base_prefix', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'get_coroutine_wrapper', 'getallocatedblocks', 'getcheckinterval', 'getdefaultencoding', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'getswitchinterval', 'gettrace', 'getwindowsversion', 'hash_info', 'hexversion', 'implementation', 'int_info', 'intern', 'is_finalizing', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'set_coroutine_wrapper', 'setcheckinterval', 'setprofile', 'setrecursionlimit', 'setswitchinterval', 'settrace', 'stderr', 'stdin', 'stdout', 'thread_info', 'version', 'version_info', 'warnoptions', 'winver']12

dir() 不會列出內置函數和變數名。如果你想列出這些內容,它們在標準模組 builtins 中定義:

需要注意的是使用 from package import item 方式導入包時,這個子項(item)既可以是包中的一個子模組(或一個子包),也可以是包中定義的其它命名,像函數、類或變數。import 語句首先核對是否包中有這個子項,如果沒有,它假定這是一個模組,並嘗試載入它。如果沒有找到它,會引發一個 ImportError 異常。

相反,使用類似 import item.subitem.subsubitem 這樣的語法時,這些子項必須是包,最後的子項可以是包或模組,但不能是前面子項中定義的類、函數或變數

2、包內引用

如果包中使用了子包結構(就像示例中的 sound 包),可以按絕對位置從相鄰的包中引入子模組。例如,如果 sound.filters.vocoder 包需要使用 sound.effects 包中的 echo 模組,它可以 from sound.Effects import echo。

你可以用這樣的形式 from module import name 來寫顯式的相對位置導入。那些顯式相對導入用點號標明關聯導入當前和上級包。以 surround 模組為例,你可以這樣用:

未完待續!

三、dir() 函數

內置函數 dir() 用於按模組名搜索模組定義,它返回一個字串類型的存儲清單:

結果如下:

['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'fib', 'fib2']['__displayhook__', '__doc__', '__excepthook__', '__interactivehook__', '__loader__', '__name__', '__package__', '__spec__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_debugmallocstats', '_getframe', '_home', '_mercurial', '_xoptions', 'api_version', 'argv', 'base_exec_prefix', 'base_prefix', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'get_coroutine_wrapper', 'getallocatedblocks', 'getcheckinterval', 'getdefaultencoding', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'getswitchinterval', 'gettrace', 'getwindowsversion', 'hash_info', 'hexversion', 'implementation', 'int_info', 'intern', 'is_finalizing', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'set_coroutine_wrapper', 'setcheckinterval', 'setprofile', 'setrecursionlimit', 'setswitchinterval', 'settrace', 'stderr', 'stdin', 'stdout', 'thread_info', 'version', 'version_info', 'warnoptions', 'winver']12

dir() 不會列出內置函數和變數名。如果你想列出這些內容,它們在標準模組 builtins 中定義:

需要注意的是使用 from package import item 方式導入包時,這個子項(item)既可以是包中的一個子模組(或一個子包),也可以是包中定義的其它命名,像函數、類或變數。import 語句首先核對是否包中有這個子項,如果沒有,它假定這是一個模組,並嘗試載入它。如果沒有找到它,會引發一個 ImportError 異常。

相反,使用類似 import item.subitem.subsubitem 這樣的語法時,這些子項必須是包,最後的子項可以是包或模組,但不能是前面子項中定義的類、函數或變數

2、包內引用

如果包中使用了子包結構(就像示例中的 sound 包),可以按絕對位置從相鄰的包中引入子模組。例如,如果 sound.filters.vocoder 包需要使用 sound.effects 包中的 echo 模組,它可以 from sound.Effects import echo。

你可以用這樣的形式 from module import name 來寫顯式的相對位置導入。那些顯式相對導入用點號標明關聯導入當前和上級包。以 surround 模組為例,你可以這樣用:

未完待續!

同類文章
Next Article
喜欢就按个赞吧!!!
点击关闭提示