从模块导入所有名称

from module_name import *

例如:

from math import *
sqrt(2)    # instead of math.sqrt(2)
ceil(2.7)  # instead of math.ceil(2.7)

这会将 math 模块中定义的所有名称导入全局命名空间,而不是以下划线开头的名称(表示编写者认为它仅供内部使用)。

警告 :如果已定义或导入具有相同名称的功能,则将覆盖该功能。几乎总是只导入特定名称 from math import sqrt, ceil推荐的方式

def sqrt(num):
    print("I don't know what's the square root of {}.".format(num))

sqrt(4)
# Output: I don't know what's the square root of 4.

from math import * 
sqrt(4)
# Output: 2.0

仅在模块级别允许加星标的导入。尝试在类或函数定义中执行它们会导致 SyntaxError

def f():
    from math import *

class A:
    from math import *

都失败了:

SyntaxError: import * only allowed at module level