简体   繁体   English

全局函数,从不同文件调用的变量

[英]Global function, variable called from different files

I have 2 different files: One is from CI build: build.py 我有2个不同的文件:一个来自CI构建:build.py

ABC_ACTIVATE = False
def activate_abc():
    global ABC_ACTIVATE
    ABC_ACTIVATE = True
    # Maybe some more very long code.

One is from customize customize.py 一种是来自customize custom.py

from build import *

activate_abc()
print ABC_ACTIVATE

The idea is customize the activation for each environment by 1 function instead of very long code. 这个想法是通过1个函数(而不是很长的代码)为每个环境自定义激活。 But it doesn't work since ABC_ACTIVATE is always False . 但这不起作用,因为ABC_ACTIVATE始终为False

It seems that the global variable cannot receive the same context in the other file. 似乎全局变量不能在另一个文件中接收相同的上下文。 Potentially some "cyclical dependencies" problem. 可能存在一些“周期性依赖”问题。

So my question is: Is there any better structure solution? 所以我的问题是:有没有更好的结构解决方案? The idea is still activate by a function and customize.py would be the last setting for apache build. 这个想法仍然可以通过一个函数来激活,而customize.py将是apache构建的最后一个设置。

The global seem cannot receive the same context in the other files. 全局似乎无法在其他文件中接收相同的上下文。 Maybe some "cyclical dependencies" problem. 也许是一些“周期性依赖”问题。

Once you imported it, ABC_ACTIVATE becomes local in the context of that script. 导入后, ABC_ACTIVATE在该脚本的上下文中变为本地。 Therefore, mutating the variable in build.py won't reflect in your other module. 因此, build.py的变量不会反映在您的其他模块中。

So my question is: Is there any better structure solution? 所以我的问题是:有没有更好的结构解决方案?

One thing you could is create an intermediary method that return the ABC_ACTIVATE Boolean in your build.py . 有一件事你可能是创建一个返回的中介方法ABC_ACTIVATE在布尔build.py

def is_abc_activated():
    return ABC_ACTIVATE

and then importing it like so, 然后像这样导入

from build import activate_abc, is_abc_activated

print(is_abc_activated())
activate_abc()
print(is_abc_activated())

>>>>
False
True

Basically, this will remove your unconditional import from build import * which is an anti-idiom in Python. 基本上,这会将您的无条件导入from build import *删除,这是Python中的反习惯用法。 Also, it will increase readability since accessing ABC_ACTIVATE can be confusing on what exactly you're doing. 另外,由于访问ABC_ACTIVATE可能会使您对您的操作感到困惑,因此它将提高可读性。

After some discussion, my friend found a quite hack solution for it: 经过一番讨论,我的朋友找到了一个不错的解决方案:

build.py: build.py:

ABC_ACTIVATE = False
def activate_abc(other_context):
    other_context.ABC_ACTIVATE = True

And in customize.py: 并在custom.py中:

from build import *
activate_abc(sys.modules[__name__])
print ABC_ACTIVATE

It works now. 现在可以使用了。

That looks like incorrect syntax for a function definition in build.py: the first { should be a : and the second } is not needed as python uses indentation to signify code blocks 对于build.py中的函数定义,这看起来像语法不正确:第一个{应该是:,而第二个}则不需要,因为python使用缩进来表示代码块

ACTIVATE = False
def activate():
    global ACTIVATE
    ACTIVATE = True

Maybe you could also do... 也许你也可以做...

import build
build.activate() 

...As when the script in build.py uses the variable in the same file whereas your imported variable is a different variable since its being imported to the current file's namespace. ...就像build.py中的脚本使用同一文件中的变量,而您导入的变量是不同的变量一样,因为它被导入到当前文件的名称空间中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM