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

Câu lệnh from...import trong Python

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

>>> 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