由外部模块分配的 Python 变量可以打印,但不能在目标模块中分配

发布于 2024-07-14 14:18:56 字数 1102 浏览 7 评论 0原文

我有两个文件,一个位于 Web 根目录中,另一个是位于 Web 根目录上方文件夹中的引导程序(顺便说一句,这是 CGI 编程)。

Web根目录中的索引文件导入引导程序并为其分配一个变量,然后调用一个函数来初始化应用程序。 到这里为止一切都按预期进行。

现在,在引导文件中,我可以打印变量,但是当我尝试为变量赋值时,会抛出错误。 如果删除赋值语句,则不会引发任何错误。

我真的很好奇在这种情况下范围界定是如何运作的。 我可以打印该变量,但无法分配给它。 这是 Python 3 上的。

index.py

# Import modules
import sys
import cgitb;

# Enable error reporting
cgitb.enable()
#cgitb.enable(display=0, logdir="/tmp")

# Add the application root to the include path
sys.path.append('path')

# Include the bootstrap
import bootstrap

bootstrap.VAR = 'testVar'

bootstrap.initialize()

bootstrap.py

def initialize():
    print('Content-type: text/html\n\n')
    print(VAR)
    VAR = 'h'
    print(VAR)

谢谢。

编辑:错误消息

UnboundLocalError: local variable 'VAR' referenced before assignment 
      args = ("local variable 'VAR' referenced before assignment",) 
      with_traceback = <built-in method with_traceback of UnboundLocalError object at 0x00C6ACC0>

I have two files, one is in the webroot, and another is a bootstrap located one folder above the web root (this is CGI programming by the way).

The index file in the web root imports the bootstrap and assigns a variable to it, then calls a a function to initialize the application. Everything up to here works as expected.

Now, in the bootstrap file I can print the variable, but when I try to assign a value to the variable an error is thrown. If you take away the assignment statement no errors are thrown.

I'm really curious about how the scoping works in this situation. I can print the variable, but I can't asign to it. This is on Python 3.

index.py

# Import modules
import sys
import cgitb;

# Enable error reporting
cgitb.enable()
#cgitb.enable(display=0, logdir="/tmp")

# Add the application root to the include path
sys.path.append('path')

# Include the bootstrap
import bootstrap

bootstrap.VAR = 'testVar'

bootstrap.initialize()

bootstrap.py

def initialize():
    print('Content-type: text/html\n\n')
    print(VAR)
    VAR = 'h'
    print(VAR)

Thanks.

Edit: The error message

UnboundLocalError: local variable 'VAR' referenced before assignment 
      args = ("local variable 'VAR' referenced before assignment",) 
      with_traceback = <built-in method with_traceback of UnboundLocalError object at 0x00C6ACC0>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

不及他 2024-07-21 14:18:56

试试这个:


def initialize():
    global VAR
    print('Content-type: text/html\n\n')
    print(VAR)
    VAR = 'h'
    print(VAR)

没有“全局VAR”python想使用局部变量VAR并给你“UnboundLocalError:赋值前引用的局部变量'VAR'”

try this:


def initialize():
    global VAR
    print('Content-type: text/html\n\n')
    print(VAR)
    VAR = 'h'
    print(VAR)

Without 'global VAR' python want to use local variable VAR and give you "UnboundLocalError: local variable 'VAR' referenced before assignment"

断念 2024-07-21 14:18:56

不要将其声明为全局的,而是传递它并在需要新值时返回它,如下所示:

def initialize(a):
    print('Content-type: text/html\n\n')
    print a
    return 'h'

----

import bootstrap
b = bootstrap.initialize('testVar')

Don't declare it global, pass it instead and return it if you need to have a new value, like this:

def initialize(a):
    print('Content-type: text/html\n\n')
    print a
    return 'h'

----

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