Python學習系列 第五章 模組

2022-08-03 17:24:20 字數 644 閱讀 7182

5.1 如何引入模組

在python中用關鍵字import來引入某個模組,比如要引用模組math,就可以在檔案最開始的地方用import math來引入。在呼叫math模組中的函式時,必須這樣引用:

模組名.函式名

有時候我們只需要用到模組中的某個函式,只需要引入該函式即可,此時可以通過語句

from 模組名 import 函式名1,函式名2....

5.2 如何定義自己的模組

在python中,每個python檔案都可以作為乙個模組,模組的名字就是檔案的名字。

比如有這樣乙個檔案test.py,在test.py中定義了函式add:

#test.py

def add(a,b):

return a+b

那麼在其他檔案中就可以先import test,然後通過test.add(a,b)來呼叫了,當然也可以通過from test import add來引入。(例子**於

python第五章 Python學習(第五章)

記錄所有的名片字典 card list defshow menu 顯示資訊 print 50 print 歡迎使用 名片管理系統 v1.0 print print 1.新增名片 print 2.顯示全部 print 3.搜尋名片 print print 0.退出系統 print 50 defnew ...

python學習第五章

1.把某件事作為另一件事匯入 import somemodule或from somemodule import somefunction或者from somemodule import somefunction,anotherfunction,yetanotherfunction或者from som...

python學習筆記第五章

1.物件是屬性和方法的集合。同樣也有繼承機制。init 就是python的建構函式。變數私有化是在變數前加上 繼承機制 class cat def hello self print i m a cat class dog cat def eat self print i eat bone kitty...