Python入門教程(二十六)Python的模塊
什么是模塊?
請思考與代碼庫類似的模塊。
模塊是包含一組函數(shù)的文件,希望在應(yīng)用程序中引用。
創(chuàng)建模塊
如需創(chuàng)建模塊,只需將所需代碼保存在文件擴展名為 .py 的文件中:
實例
在名為 mymodule.py 的文件中保存代碼:
def greeting(name): print("Hello, " + name)
使用模塊
現(xiàn)在,我們就可以用 import 語句來使用我們剛剛創(chuàng)建的模塊:
實例
導入名為 mymodule 的模塊,并調(diào)用 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 關(guān)鍵字創(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 關(guān)鍵字選擇僅從模塊導入部件。
實例
名為 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 關(guān)鍵字導入時,請勿在引用模塊中的元素時使用模塊名稱。示例:person1[“age”],而不是 mymodule.person1[“age”]。
到此這篇關(guān)于Python入門教程(二十六)Python的模塊的文章就介紹到這了,更多相關(guān)Python的模塊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python Matplotlib畫圖之調(diào)整字體大小的示例
本篇文章主要介紹了python Matplotlib畫圖之調(diào)整字體大小的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11python 數(shù)據(jù)生成excel導出(xlwt,wlsxwrite)代碼實例
這篇文章主要介紹了python 數(shù)據(jù)生成excel導出(xlwt,wlsxwrite)代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-08-08