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

Cannot rebind an imported name in python 3

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

我正在尝试在 Python 3:

中重新绑定导入函数的名称

例如

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

但是,由于某些原因,我无法做到这一点。模块中的imported_function和非模块中的调用函数:

imported_function = another_function(2)

旁注:此处,another_function(2) 是在模块外部定义的函数。 Imported_function 一直给我模块中的 imported_function,即使在赋值语句 imported_function = function(5).

之后也是如此

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

如果我理解正确,你可以在你的情况下使用 import ... as。例如,如果您希望 imported_functionanother_function,则执行此操作:

from module import imported_function as another_function

another_function(...)

或者您可以将函数重新分配给另一个名称:

another_function = imported_function

但是,如果您这样做:

another_function = imported_function(2)

根据 imported_function 的定义,它将 return 值或 None 到变量 another_function。括号很重要。