Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Import một hàm đơn lẻ từ module Python

Dùng câu lệnh from module import function để import hàm cụ thể từ module Python. Ví dụ, import hàm sin từ thư viện math không import hàm khác:

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

Lưu ý: không phải prefix sin với math. vì chỉ sin được import, không phải math. Cũng có thể alias imported function. Ví dụ:

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