gpt4 book ai didi

python - 为什么 "import"是一个语句,而 "reload"是一个函数?

转载 作者:太空狗 更新时间:2023-10-29 18:32:24 26 4
gpt4 key购买 nike

我知道如何使用两者,但我很好奇为什么决定让一个成为声明而另一个成为函数。

最佳答案

首先,您可以使用函数导入,来自 importlib's documentation :

The __import__() function
The import statement is syntactic sugar for this function.

例如这两个语句是等价的:

from random import randint as random_int

random_int = __import__("random").randint

然而,import 语句从替代语法中受益匪浅,因为 reload 实际上没有任何替代含义。

如果 reload 是它自己的语句,我也可以想象很多新手程序员会犯这个错误:

from random import *
reload random #does not affect the current namespace!

由于reload 函数需要一个模块(不是用from _ import * 推导出来的)编码人员可能想知道为什么不重新加载导入的名称。与this answer有关

关于python - 为什么 "import"是一个语句,而 "reload"是一个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36841844/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com