Python 中“from...import”語句有什麼作用?


“from 模組 import 函式”語句用於從 Python 模組中匯入特定的函式。例如,如果您想從 math 庫中匯入 sin 函式,而不匯入任何其他函式,您可以按如下方式執行:

>>> from math import sin
>>> sin(0)
0.0

請注意,您不必在 sin 前新增“math.”,因為只匯入了 sin,而不是 math。您還可以為匯入的函式指定別名。例如:

>>> from math import cos as cosine
>>> cosine(0)
1.0

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告