对于确实存在的字典,pdb下出现"NameError:未定义全局名称" [英] 'NameError: global name is not defined' under pdb, for dictionary that does exist

查看:281
本文介绍了对于确实存在的字典,pdb下出现"NameError:未定义全局名称"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在lambda函数中遇到了有关范围问题.我可以成功将foo输出到stdout,但是使用max()包括lambda时出现错误-请参见下面的简化代码...

I've encountered an issue re scopes in a lambda function. I can successfully output foo to stdout but I get an error when using max() including a lambda - see simplified code below...

总而言之,我正在尝试在未知数量的一阶键中找到嵌套键budget的最大值.

All in all, I am trying find the largest value for a nested key budget within an unknown number of first order keys.

(Pdb) foo = self.some_method()    # some_method() returns a dict, printed in the next step

(Pdb) pp foo

{'1': {'count': 1,
       'extra_data': {'activity-count': 1,
                             'budget': 0,
                             [...MORE KEY-VALUE PAIRS HERE...]
                             'version': 1},
       [...LOTS MORE KEY-VALUE PAIRS HERE...]
       'elements_total': defaultdict(<type 'int'>, {'result': 1, 'another_key': 2}),
       'extra_year_data': defaultdict(<function <lambda> at 0x10e05bd70>, {})}, 

 '2': {'count': 1,
       'extra_data': {'activity-count': 1,
                             'budget': 3,
                             [...MORE KEY-VALUE PAIRS HERE...]
                             'version': 1},
       [...LOTS MORE KEY-VALUE PAIRS HERE...]
       'elements_total': defaultdict(<type 'int'>, {'result': 1, 'another_key': 2}),
       'extra_year_data': defaultdict(<function <lambda> at 0x10e05bd70>, {})}}

(Pdb) max(foo, key=lambda x: foo[x]['extra_data']['budget'])
*** NameError: global name 'foo' is not defined

总而言之,我正在尝试使用max(foo, key=lambda x: foo[x]['extra_data']['budget'])在未知数量的一阶键中找到嵌套键budget的最大值.

All in all, I am trying to use max(foo, key=lambda x: foo[x]['extra_data']['budget']) to find the largest value for a nested key budget within an unknown number of first order keys.

在这种情况下,预期结果可能是2作为foo['2']['extra_data']['budget'] = 3foo['1']['extra_data']['budget'] = 0的值.

The expected result in this case could be 2 as the value for foo['2']['extra_data']['budget'] = 3 vs. foo['1']['extra_data']['budget'] = 0.

该错误是否与某些(不相关的)键中包含defaultdict的事实有关?

Could the error be related to the fact that some of the (unrelated) keys have defaultdicts within them?

推荐答案

您使用pdb设置了新的 local ,但是在此调试器会话中使用嵌套作用域的表达式不可见.嵌套范围内的任何表达式 (例如lambda用于key参数),使用当前帧本地的名称,都将需要关闭,并且会出现此问题.

You set a new local with pdb, but that is not visible to expressions using nested scopes in this debugger session. Any expression in a nested scope such as the lambda used for the key argument, using a name that is local to the current frame, would need to be a closure and will have this problem.

这是调试器和Python编译方式的局限性;仅当需要生成闭包的函数在同一会话中编译时,才可以创建闭包.由于正在调试的函数是在没有foo作为闭包的情况下进行编译的,因此lambda表达式无法将其本身使用.

That's a limitation of how the debugger and Python compilation work; closures can only be created if the function that need to produce them was compiled in the same session. Since the function you are debugging was compiled without foo being a closure, it cannot be used by the lambda expression as such.

您可以将本地变量绑定到lambda(使其成为本地变量而不是闭包):

You can bind the local to the lambda (making it a local rather than a closure):

max(foo, key=lambda x, foo=foo: foo[x]['extra_data']['budget'])

请参见 obj .__ closure__到底包含什么?有关Python编译器如何创建闭包的详细信息.

See What exactly is contained within a obj.__closure__? for details on how the Python compiler creates closures.

这篇关于对于确实存在的字典,pdb下出现"NameError:未定义全局名称"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆