如何向我导入的模块添加变量?
我想做的是这样的:
template.py
def dummy_func():
print(VAR)
# more functions like this to follow
fabfile.py
# this gets called by fabric (fabfile.org)
# safe to think of it as ant build.xml
import template
template.VAR = 'some_val'
from template import *
即我有一个模板模块,其他模块应该“扩展”提供所需的变量。 这可以以函数方式完成(与对象继承相反)吗?
编辑:添加了更多代码。
What I want to do is something like this:
template.py
def dummy_func():
print(VAR)
# more functions like this to follow
fabfile.py
# this gets called by fabric (fabfile.org)
# safe to think of it as ant build.xml
import template
template.VAR = 'some_val'
from template import *
Namely I have a template module other modules should 'extend' contributing the required variables.
Can this be done in a functional manner (as opposed to object inheritance)?
EDIT: Added a bit more code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定你所说的“函数式方式”是什么意思——你的意思是,如 函数式编程?这不会发生(因为你本质上是在尝试修改一个对象,这与 FP 相反)。或者你的意思是“一种有效的方式”?
对于后一种解释,最大的问题是 import * 部分——在建议不要使用它的许多问题中,您将遇到一个问题:它执行快照 当时绑定的任何模块级名称(或者只是模块中的
__all__
中列出的名称,如果已定义)——将来对名称绑定的更改将永远不会反映出来在之前执行过import *
的模块中。为什么您认为需要将
template_module
的命名空间合并到导入模块的命名空间中?如果您只是执行了常规的import template_module as tm
,那么只需将所有相关名称引用为tm.this
、tm.that
即可。很好(包括拾取到使用瞬间为止对绑定的所有更改 - 换句话说,它使用您在这里似乎需要的“后期绑定”方法)。I'm not sure what you mean by "a functional manner" -- do you mean, as in functional programming? That's not going to happen (since you're intrinsically trying to modify an object, which is the reverse of FP). Or do you mean something like "a way that works"?
For the latter interpretation, the big problem is the
import *
part -- among the many problems that suggest not using that, you're going to be running smack into one: it performs a snapshot of whatever module-level names are bound at the time (or just those listed in__all__
in the module, if that's defined) -- future changes to the bindings of names will never be reflected in modules that previously did theimport *
.Why do you believe you need to merge the
template_module
's namespace into that of the importing module? If you just did a regularimport template_module as tm
, then simply referring to all the relevant names astm.this
,tm.that
will work just fine (including picking up all changes to the bindings up to the instant of use -- in other words, it uses the "late binding" approach that you appear to require here).如果您在一个地方更改模块的属性,那么其他地方也将是相同的。证明:
创建一个文件“/tmp/test1.py”:
然后
现在 os.path 即使在主应用程序中也是一个空字符串,并且 zzz 也无处不在。
If you change a property of a module in one place, it will be the same in other places too. Proof:
Create a file '/tmp/test1.py':
Then
Now os.path is an empty string even in the main application, and zzz is everywhere too.
事实证明,这是一个以结构为中心的解决方案。
因此,您有一个抽象的 some__fab__template.py 和一个具体的 fabfile.py,它们应该“扩展”模板,提供一些必需的变量(例如项目名称)。
我已经利用 fab 的 env 字典实现了它。
在模板文件中,您引用
env.VAR
,并在“具体”fabfile.py 中执行以下操作:Turns out this has a fabric-centric solution.
So you have an abstract some__fab__template.py and a concrete fabfile.py that should 'extend' the template contributing some required variables (e.g. project name).
I've implemented it utilizing fab's env dictionary.
In template file you reference
env.VAR
and in the 'concrete' fabfile.py you do this: