如何從Python模組匯入單個函式?
你可以使用“from module import function”語句從Python模組中匯入一個特定的函式。例如,如果你想從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
廣告