【发布时间】:2018-07-22 19:47:22
【问题描述】:
我有一个名为 foo.py 的模块,另一个模块 bar.py 从该模块将函数加载到其命名空间中。如何在 foo.py 中为所有函数添加前缀 foo_。一个例子:
foo.py:
def func1():
return "hello"
def func2():
return "world"
bar.py:
from foo import *
def do_something():
return foo_func1()
【问题讨论】:
-
为什么不直接
import foo然后foo.func1()?这是使用模块的正确方法,并且看起来与您所追求的非常相似。 -
不要使用星号导入,这样就不会有这个名字冲突的问题。见stackoverflow.com/questions/2386714/why-is-import-bad
-
Namespaces are one honking great idea -- let's do more of those!-- The Zen of Python.
标签: python