Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何访问_umain _;范围内的全局变量?_Python_Namespaces - Fatal编程技术网

Python 如何访问_umain _;范围内的全局变量?

Python 如何访问_umain _;范围内的全局变量?,python,namespaces,Python,Namespaces,我对python中变量的名称空间和范围感到困惑 假设我有一个test.py: # -*- coding: utf-8 -*- """ @author: jason """ if __name__ == '__main__': global strName print strName 然后,我定义了一个名为strName的变量,并尝试在test.py中访问它,但它抛出了一个错误: In [9]: strName = "Joe" In [10]: run test.py hel

我对python中变量的名称空间和范围感到困惑

假设我有一个test.py:

# -*- coding: utf-8 -*-
"""
@author: jason
"""

if __name__ == '__main__':
    global strName
    print strName
然后,我定义了一个名为strName的变量,并尝试在test.py中访问它,但它抛出了一个错误:

In [9]: strName = "Joe"

In [10]: run test.py hello
---------------------------------------------------------------------------  NameError                              Traceback (most recent call last)  C:\Anaconda\lib\site-packages\IPython\utils\py3compat.pyc in execfile(fname, glob, loc)
    195             else:
    196                 filename = fname
--> 197             exec compile(scripttext, filename, 'exec') in glob, loc
    198     else:
    199         def execfile(fname, *where):

d:\playground\test.py in <module>()
     13         print "hello"
     14         global strName
---> 15         print strName
     16 

NameError: global name 'strName' is not defined

In [11]:
[9]中的
:strName=“Joe”
在[10]中:运行test.py hello
---------------------------------------------------------------------------NameError回溯(最近一次调用上次)C:\Anaconda\lib\site packages\IPython\utils\py3compat.pyc在execfile(fname,glob,loc)中
195其他:
196 filename=fname
-->197 exec编译(脚本文本,文件名,'exec'),在glob,loc中
198其他:
199 def execfile(fname,*其中):
d:\playway\test.py在()中
13打印“你好”
14全局strName
--->15打印strName
16
NameError:未定义全局名称“strName”
在[11]中:

我想知道为什么会发生这种情况,有没有办法访问test.py中的strName?

Global专门用于在方法外部定义变量,并且希望在该方法内部使用它而不传入参数的情况。它放在方法的顶部,以便python将该变量视为全局变量,而不是具有相同名称的新局部变量。Global不是一种声明变量的方法,由于strName不存在,Global无法确定strName的位置。

test.py:

strName = "John Doe"
print strName
$ python
>>> from test import strName
>>> print strName
John Doe
交互式Shell:

strName = "John Doe"
print strName
$ python
>>> from test import strName
>>> print strName
John Doe

global
不是全局的<代码>全局为模块级;真正的全局变量,如
min
int
存在于
\uuuuuuuuuuuuu
模块中(
Python 3中的内置变量)。在模块级使用
全局
声明是多余的

我强烈建议您以另一种方式将数据传递到
test.py
,例如在其中定义函数并将字符串作为参数传递:

test.py:

def print_thing(thing):
    print thing
要使用test.py的其他代码:

import test
test.print_thing("Joe")

A.您没有定义
strName
。B您需要导入
测试
。e、 g:
如果
语句没有创建新的作用域(并且在模块作用域中,
全局
没有意义),则从test import strName
。在Python 3中,为了避免混淆,这被更改为
非局部
。@JamesMills:没有<代码>全局
仍然存在<代码>非局部
专门用于引用外部但非全局范围的变量。您可以在中看到,定义不允许将其用于全局变量。我认为这没有抓住要点,因为他希望模块从用户处访问
strName
。是的,我可以看到这一点——但是OP试图做的事情显然离不开一些魔法:)@James Mills谢谢,但我仍然想知道,如果我在shell中输入strName='Joe'来定义strName,它的范围是什么?我如何在一个模块中访问这个变量(除了将strName作为函数的参数传递外)?正是您所说的(后面的部分)。您应该将变量作为参数传递给函数。我甚至不确定将
strName
声明为全局变量在您描述的用例中是否有效。我还没有测试过,也永远不会测试,但我相信:
globals().update({strName':'Joe')
应该有效。虽然,仅此评论就足以让我对这句话所体现的纯粹邪恶感到愤怒,但在这种情况下,@anon:
globals
也是模块级的<代码>导入\uuuu内置\uuuuuu__builtin\uuuu.strName=“Joe”
可以工作,尽管这只是在紧急情况下才做的事情。