假设我有一个函数
foo
在具有命名参数的大型python项目中
bar
以下内容:
def foo(bar=42):
do_something_with_bar(bar)
在代码库中,在使用或省略
酒吧
参数。
现在假设我正在更改这个函数,因为它有一个bug,并且希望开始使用某个包中的函数,这个包碰巧也被调用
酒吧
是的。我不能用
酒吧
当我像这样导入时的函数:
from package import bar
def foo(bar=42):
# how can I use the package.bar function here?
do_something_with_bar(bar)
我可以用这样的东西:
from package import bar as package_bar
但是这个文件也包含了很多调用
酒吧
所以这是不允许的。
我能看到这个工作的唯一方法是将
酒吧
方法来自
package
以下内容:
from package import bar
package_bar = bar
def foo(bar=42):
do_something_with_bar(package_bar(bar))
或者多次导入(未测试):
from package import bar, bar as package_bar
def foo(bar=42):
do_something_with_bar(package_bar(bar))
有什么办法我可以重新命名
酒吧
中的参数
福
函数,而不调用
福
整个代码库中的函数中断?