Python 中“from...import”语句有什么作用?


“from module import function”语句用于从 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

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.