如何向我导入的模块添加变量?

发布于 2024-08-14 22:35:08 字数 364 浏览 5 评论 0原文

我想做的是这样的:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

德意的啸 2024-08-21 22:35:08

我不确定你所说的“函数式方式”是什么意思——你的意思是,如 函数式编程?这不会发生(因为你本质上是在尝试修改一个对象,这与 FP 相反)。或者你的意思是“一种有效的方式”?

对于后一种解释,最大的问题是 import * 部分——在建议不要使用它的许多问题中,您将遇到一个问题:它执行快照 当时绑定的任何模块级名称(或者只是模块中的 __all__ 中列出的名称,如果已定义)——将来对名称绑定的更改将永远不会反映出来在之前执行过 import * 的模块中。

为什么您认为需要将 template_module 的命名空间合并到导入模块的命名空间中?如果您只是执行了常规的 import template_module as tm,那么只需将所有相关名称引用为 tm.thistm.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 the import *.

Why do you believe you need to merge the template_module's namespace into that of the importing module? If you just did a regular import template_module as tm, then simply referring to all the relevant names as tm.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).

作妖 2024-08-21 22:35:08

如果您在一个地方更改模块的属性,那么其他地方也将是相同的。证明:

创建一个文件“/tmp/test1.py”:

imoprt os
os.path = '' # set os.path (module) to a mere string
os.zzz = 'zzz'

然后

cd /tmp && python

>>> dir(test)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'os', 'z']
>>> test.os
<module 'os' from '/usr/lib/python2.6/os.pyc'>
>>> test.os.path
''
>>> import os
>>> os.path
''
>>> os.zzz
'zzz'

现在 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':

imoprt os
os.path = '' # set os.path (module) to a mere string
os.zzz = 'zzz'

Then

cd /tmp && python

>>> dir(test)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'os', 'z']
>>> test.os
<module 'os' from '/usr/lib/python2.6/os.pyc'>
>>> test.os.path
''
>>> import os
>>> os.path
''
>>> os.zzz
'zzz'

Now os.path is an empty string even in the main application, and zzz is everywhere too.

梦幻的心爱 2024-08-21 22:35:08

事实证明,这是一个以结构为中心的解决方案。
因此,您有一个抽象的 some__fab__template.py 和一个具体的 fabfile.py,它们应该“扩展”模板,提供一些必需的变量(例如项目名称)。
我已经利用 fab 的 env 字典实现了它。
在模板文件中,您引用env.VAR,并在“具体”fabfile.py 中执行以下操作:

from fabric.api import *
env.VAR = 'some value'
import some__fab__template

def dist():
    some__fab__template.dist()

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:

from fabric.api import *
env.VAR = 'some value'
import some__fab__template

def dist():
    some__fab__template.dist()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文