-
-
Notifications
You must be signed in to change notification settings - Fork 32k
Variable Not Defined When Using Dictionary Comprehension Inside a Function Scope [Pdb] #100185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This can be reproduced with just def somefunc(fltrlist):
d1 = {"A": [1, 2, 3]}
source = '{k: v for k, v in d1.items() if k in fltrlist}'
code = compile(source + '\n', '<stdin>', 'single')
exec(code, globals(), locals())
klist = ["A", "B"]
somefunc(klist) Outputs:
|
Looks like this happens because
|
One more note: this happens on all versions of python, starting from 3.6 (I don't have any earlier versions installed). And even 2.7 🥼 |
@sobolevn I think it is impossible to fix without reimplementing this builtin function due to |
https://docs.python.org/3/reference/expressions.html#displays-for-lists-sets-and-dictionaries explains:
Note that the global scope is always available. @e-baumer It would have been better to have asked for an explanation under Help at https://discuss.python.org/. The docs should be consulted before reporting puzzling behavior as a bug. |
Bug report
I receive a
NameError
for a variable defined in a function scope when trying to execute a dictionary comprehension in pdb. The variable is part of the local scope as seen withlocals()
For the following code:
When I enter pdb and try to run the dictionary comprehension I get the following:
The variable is part of the local scope,
If I try the same thing but not defined within a function it works.
When in pdb the dictionary comprehension works fine.
Your environment
The text was updated successfully, but these errors were encountered: