2024-04-26 12:35:14 发布
网友
你有两个选择:
import math math.sqrt()
将math模块导入其自己的命名空间。这意味着函数名必须以math作为前缀。这是一个很好的实践,因为它避免了冲突,并且不会覆盖已经导入到当前命名空间中的函数。
math
或者:
from math import * sqrt()
将从math模块中的所有内容导入当前命名空间。That can be problematic。
如果只需要import math调用sqrt函数,则需要执行以下操作:
import math
sqrt
In [1]: import math In [2]: x = 2 In [3]: math.sqrt(x) Out[3]: 1.4142135623730951
这是因为from math import sqrt为您带来了sqrt函数,但是import math只为您带来了模块。
from math import sqrt
当您只使用import math时,sqrt函数将以不同的名称传入:math.sqrt。
math.sqrt
你有两个选择:
将
math
模块导入其自己的命名空间。这意味着函数名必须以math
作为前缀。这是一个很好的实践,因为它避免了冲突,并且不会覆盖已经导入到当前命名空间中的函数。或者:
将从
math
模块中的所有内容导入当前命名空间。That can be problematic。如果只需要
import math
调用sqrt
函数,则需要执行以下操作:这是因为
from math import sqrt
为您带来了sqrt
函数,但是import math
只为您带来了模块。当您只使用
import math
时,sqrt
函数将以不同的名称传入:math.sqrt
。相关问题 更多 >
编程相关推荐