使用列表生成器时,Python 3中的pdb模块中可能存在错误 [英] Possible bug in pdb module in Python 3 when using list generators

查看:133
本文介绍了使用列表生成器时,Python 3中的pdb模块中可能存在错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python 3中运行此代码后,

After running this code in Python 3:

import pdb

def foo():
    nums = [1, 2, 3]
    a = 5
    pdb.set_trace()

foo()

以下表达式起作用:

(Pdb) print(nums)
[1, 2, 3]

(Pdb) print(a)
5

(Pdb) [x for x in nums]
[1, 2, 3]

,但是以下表达式失败:

but the following expression fails:

(Pdb) [x*a for x in nums]
*** NameError: global name 'a' is not defined

以上在Python 2.7中正常工作.

The above works fine in Python 2.7.

这是一个错误还是我遗漏了一些东西?

Is this a bug or I am missing something?

更新:查看新接受的答案.这确实是一个错误(或有问题的设计),现在已通过在pdb中引入新的命令和模式来解决.

Update: See the new accepted answer. This was indeed a bug (or a problematic design) which has been addressed now by introducing a new command and mode in pdb.

推荐答案

如果在[i] pdb会话中键入interact,则会得到一个交互式会话,并且列表理解在此模式下可以正常工作

if you type interact in your [i]pdb session, you get an interactive session, and list comprehensions do work as expected in this mode

来源: http://bugs.python.org/msg215963

这篇关于使用列表生成器时,Python 3中的pdb模块中可能存在错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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