Python入門教程(二十六)Python的模塊
什么是模塊?
請思考與代碼庫類似的模塊。
模塊是包含一組函數(shù)的文件,希望在應用程序中引用。
創(chuàng)建模塊
如需創(chuàng)建模塊,只需將所需代碼保存在文件擴展名為 .py 的文件中:
實例
在名為 mymodule.py 的文件中保存代碼:
def greeting(name):
print("Hello, " + name)使用模塊
現(xiàn)在,我們就可以用 import 語句來使用我們剛剛創(chuàng)建的模塊:
實例
導入名為 mymodule 的模塊,并調用 greeting 函數(shù):
import mymodule
mymodule.greeting("Bill")
運行實例
Hello, Bill
注釋:如果使用模塊中的函數(shù)時,請使用以下語法:
module_name.function_name
模塊中的變量
模塊可以包含已經(jīng)描述的函數(shù),但也可以包含各種類型的變量(數(shù)組、字典、對象等):
實例
在文件 mymodule.py 中保存代碼:
person1 = {
"name": "Bill",
"age": 63,
"country": "USA"
}
實例
導入名為 mymodule 的模塊,并訪問 person1 字典:
import mymodule # Python學習交流裙:708525271 a = mymodule.person1["age"] print(a)
運行實例
63
為模塊命名
您可以隨意對模塊文件命名,但是文件擴展名必須是 .py。
重命名模塊
您可以在導入模塊時使用 as 關鍵字創(chuàng)建別名:
實例 為 mymodule 創(chuàng)建別名 mx:
import mymodule as mx a = mx.person1["age"] print(a)
運行實例
63
內(nèi)建模塊
Python 中有幾個內(nèi)建模塊,您可以隨時導入。
實例
導入并使用 platform 模塊:
import platform x = platform.system() print(x)
運行實例
Windows
使用 dir() 函數(shù)
有一個內(nèi)置函數(shù)可以列出模塊中的所有函數(shù)名(或變量名)。dir() 函數(shù):
實例
列出屬于 platform 模塊的所有已定義名稱:
import platform x = dir(platform) print(x)
運行實例
['DEV_NULL', '_UNIXCONFDIR', '_WIN32_CLIENT_RELEASES', '_WIN32_SERVER_RELEASES', '__builtins__', '__cached__', '__copyright__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '__version__', '_comparable_version', '_component_re', '_default_architecture', '_dist_try_harder', '_follow_symlinks', '_ironpython26_sys_version_parser', '_ironpython_sys_version_parser', '_java_getprop', '_libc_search', '_linux_distribution', '_lsb_release_version', '_mac_ver_xml', '_node', '_norm_version', '_parse_release_file', '_platform', '_platform_cache', '_pypy_sys_version_parser', '_release_filename', '_release_version', '_supported_dists', '_sys_version', '_sys_version_cache', '_sys_version_parser', '_syscmd_file', '_syscmd_uname', '_syscmd_ver', '_uname_cache', '_ver_output', '_ver_stages', 'architecture', 'collections', 'dist', 'java_ver', 'libc_ver', 'linux_distribution', 'mac_ver', 'machine', 'node', 'os', 'platform', 'popen', 'processor', 'python_branch', 'python_build', 'python_compiler', 'python_implementation', 'python_revision', 'python_version', 'python_version_tuple', 're', 'release', 'subprocess', 'sys', 'system', 'system_alias', 'uname', 'uname_result', 'version', 'warnings', 'win32_ver']
注釋:dir() 函數(shù)可用于所有模塊,也可用于您自己創(chuàng)建的模塊。
從模塊導入
您可以使用 from 關鍵字選擇僅從模塊導入部件。
實例
名為 mymodule 的模塊擁有一個函數(shù)和一個字典:
def greeting(name):
print("Hello, " + name)
person1 = {
"name": "Bill",
"age": 63,
"country": "USA"
}
實例
僅從模塊導入 person1 字典:
from mymodule import person1 print (person1["age"])
運行實例
63
提示:在使用 from 關鍵字導入時,請勿在引用模塊中的元素時使用模塊名稱。示例:person1[“age”],而不是 mymodule.person1[“age”]。
到此這篇關于Python入門教程(二十六)Python的模塊的文章就介紹到這了,更多相關Python的模塊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python性能分析工具pyinstrument提高代碼效率
今天分享一個超級實用的 Python 性能分析工具 pyinstrument ,可以快速找到代碼運行最慢的部分,幫助提高代碼的性能。支持 Python 3.7+ 且能夠分析異步代碼,僅需一條命令即可顯示具體代碼的耗時。經(jīng)常寫 Python 的小伙伴一定要用一下2021-09-09
Python unittest 簡單實現(xiàn)參數(shù)化的方法
今天小編就為大家分享一篇Python unittest 簡單實現(xiàn)參數(shù)化的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11
windows系統(tǒng)上通過whl文件安裝triton模塊的簡單步驟
這篇文章主要介紹了在Windows系統(tǒng)中通過.whl文件安裝Triton的步驟,包括確認系統(tǒng)環(huán)境、下載合適的.whl文件、使用pip安裝、驗證安裝、使用Triton以及解決潛在問題,需要的朋友可以參考下2025-01-01

