无法在python 3中重新绑定导入的名称

2024-04-20 04:37:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python很陌生。这是我在编写程序时遇到的一个问题

我正在尝试在Python 3中重新绑定导入函数的名称:

例如

from module import imported_function
...
imported_function = function(5)

但是,由于某种原因,我无法做到这一点。模块中导入的函数和非模块中的函数称为函数:

imported_function = another_function(2)

旁注:这里,另一个函数(2)是在模块外部定义的函数。 Imported\u function不断地给我从模块中导入的\u函数,即使在赋值语句Imported\u function=function(5)之后也是如此

我不知道发生了什么事。导入语句在赋值中有更高的优先级吗


Tags: 模块函数fromimport名称定义anotherfunction
1条回答
网友
1楼 · 发布于 2024-04-20 04:37:02

如果我理解正确,您可以在您的案例中使用import ... as。例如,如果希望imported_functionanother_function,请执行以下操作:

from module import imported_function as another_function

another_function(...)

或者可以将函数重新分配给其他名称:

another_function = imported_function

但是,如果您这样做:

another_function = imported_function(2)

根据导入的函数的定义,它将返回 值或无。括号很重要

相关问题 更多 >