【问题标题】:KeyError: global not accessible through imported classKeyError:全局无法通过导入的类访问
【发布时间】:2016-01-17 18:42:50
【问题描述】:

我正在尝试计算化学方程式中元素的数量。我以某种方式创建的调试器无法访问我程序中的全局变量。具体来说,我正在尝试访问carrots,但left 没有被添加到堆栈中。有什么想法吗?

Debug.py

class Debugger(object):
    def __init__(self,objs):
        assert type(objs)==list, 'Not a list of strings'
        self.objs = objs
    def __repr__(self):
        return '<class Debugger>'
    def show(self):
        for o in self.objs:
            print o,globals()[o] #EDIT

Chemical_Balancer.py

from Debug import Debugger

def directions():
    print 'Welcome to the chem Balancer.'
    print 'Use the following example to guide your work:'
    global left #LEFT IS GLOBAL
    left = 'B^6 + C^2 + B^3 + C^3 + H^9 + O^4 + Na^1'
    print left
    print "#Please note to use a 'hat' when entering all elements"
    print '#use only one letter elements for now'
# left = raw_input('enter formula:')  #enter formula to count
directions()

chem_stats = {}
chem_names = []
chem_names = []
chem_indy = []

for c in range(len(left)):
    if left[c].isalpha() and left[c].isupper():
        chars = ''
        if left[c+1].islower():
            chars += left[c]+left[c+1]
        else:
            chars += left[c]
        #print chars
        chem_indy.append(c)
        chem_names.append(chars)

carrots = [x for x in range(len(left)) if left[x]=='^']

debug = Debugger(['carrots','chem_names','chem_indy','chem_stats']) # WITHOUT LEFT
debug.show()

错误信息:

Traceback (most recent call last):
  File "C:\Python27\#Files\repair\Chemical_Balancer.py", line 38, in <module>
    debug.show()
  File "C:\Python27\lib\site-packages\Debug.py", line 12, in show
    print o,globals()[o]
  File "<string>", line 1, in <module>
KeyError: 'carrots'

【问题讨论】:

  • 你为什么使用 eval()? (我不是在问,因为它是错误的,只是想知道为什么要使用它。)
  • 找到变量的值。
  • 是的,我用 eval() 尝试了一些东西,我想我会找到上面的文档
  • 它引发了KeyError
  • 不要使用全局变量,而是使用字典。

标签: python python-2.7 nameerror global-scope


【解决方案1】:

关于left变量的具体错误:

当你说一个变量是全局变量时,python 知道在使用它的名字时它必须在全局命名空间中查找它。但是在代码中left 并没有被分配到这样的命名空间中。

如您所见,left 被注释掉了

#left = raw_input('enter formula:')  #enter formula to count

通过删除行首的#来取消注释,因此directions函数内的行

global left

可以找到它,下面的说明可以工作。

关于实施: 一种允许调试器知道在哪里查找变量的解决方案,即在哪个模块中,可以是在创建模块时为其提供模块的名称。然后调试器对象可以通过sys.modules[module_name].__dict__访问创建它的模块的全局变量

debugger.py

import sys
class Debugger(object):
    def __init__(self, module_name, objs):
        assert type(objs)==list,'Not a list of strings'
        self.objs = objs
        self.module_name = module_name
    def __repr__(self):
        return '<class Debugger>'
    def show(self):
        for o in self.objs:
            print o, sys.modules[self.module_name].__dict__[o]

chemical_balancer.py

import debugger as deb
a = 1
b = 2
d = deb.Debugger(__name__, ['a', 'b'])
print(d.objs)
d.show()
a = 10
b = 20
d.show()

产生

['a', 'b']
a 1
b 2
a 10
b 20

如您所见,每次调用其show 方法时,调试器都会打印变量的当前值

我发现this SO Q&A 信息丰富且很有帮助。

【讨论】:

  • 在说明里面,global left;left = 'B^6 + C^2 + B^3 + C^3 + H^9 + O^4 + Na^1'
  • 我添加了一个代码示例,向您展示它确实有效
  • 我理解并且可行,但我正在从其他地方导入模块。单独的类不知道 globals()。
  • 感谢您的帮助。另一种解决方案是将全局变量作为构造函数传递,例如。 Debugger(mylist, globals()) 并让 __init__ 访问它。
  • 哦,是的,我有想过。但我认为它是一个调试器,而不是一个记录器。如果将值传递给它,它将打印出变量在创建时的值,而不是在调用 show 时。
猜你喜欢
  • 1970-01-01
  • 2017-01-17
  • 2020-01-11
  • 1970-01-01
  • 1970-01-01
  • 2013-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多