homepage

This issue tracker has been migrated to GitHub , and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Nested loop in dictionary comprehension gives `global name not defined` inside class
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 2.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder: improper scope in list comprehension, when used in class declaration
View: 3692
Assigned To: Nosy List: con-f-use, serhiy.storchaka, xtreak
Priority: normal Keywords:

Created on 2018年07月09日 15:57 by con-f-use, last changed 2022年04月11日 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
what_a_dict.py con-f-use, 2018年07月09日 15:57 More illustrative example
what_a_dict.py con-f-use, 2018年07月10日 10:56
Messages (6)
msg321326 - (view) Author: Jan Christoph (con-f-use) * Date: 2018年07月09日 15:57
The python code: 
```
class _tri(object):
 infts = '(+/-)inf, (+/-)infty, (+/-)infinity'
 strange_failing = {x+s.replace('(+/-)',''):None for x in ('+','-','') for s in infts.split(', ')}
```
gives a `global name 'infts' is not defined` exception, when normal dictionary comprehensions (without nested loops) and regular nested for-loops work perfectly well.
For a complete shell session and more illustrative example in versions 2.7.15 and 3.6.4 see: https://pastebin.ubuntu.com/p/9Pg8DThbsd/ 
msg321355 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2018年07月10日 05:36
I think this is explained in the below answers with example that the left most loop in the comprehension has access to the class variables which the nested comprehensions don't have
https://stackoverflow.com/a/22692274/2610955
https://stackoverflow.com/a/13913933/2610955
Thanks
msg321362 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018年07月10日 07:33
This looks like a duplicate of issue3692.
msg321369 - (view) Author: Jan Christoph (con-f-use) * Date: 2018年07月10日 08:32
But the simpler dictionary compprehension `{s.replace('(+/-)',''):None for s in infts.split(', ')}` works perfectly. Shouldn't that also give the error if it was a scope issue?
msg321378 - (view) Author: Jan Christoph (con-f-use) * Date: 2018年07月10日 10:53
Okay, so we're a in another scope inside the dictionary comprehension (all comprehensions for that matter), and only one symbol is passed to the inside.
That's why `strange_reversed_working = {x+s.replace('(+/-)',''):None for x in infts.split(', ') for s in ('+','-','')}` functions, but if you reverse the order it does not. That's a real trap.
msg321379 - (view) Author: Jan Christoph (con-f-use) * Date: 2018年07月10日 10:56
Updated example with reversed variable order for reference. This really seems to be related to issue3692, but really not the same thing.
IMHO both `a` and `b` should be passed in a situation like this:
````
a = range(5)
b = range(3)
c = [x+y for x in a for y in b]
````
History
Date User Action Args
2022年04月11日 14:59:02adminsetgithub: 78257
2018年07月10日 10:56:15con-f-usesetfiles: + what_a_dict.py

messages: + msg321379
2018年07月10日 10:53:01con-f-usesetmessages: + msg321378
2018年07月10日 08:32:26con-f-usesetmessages: + msg321369
2018年07月10日 07:33:11serhiy.storchakasetstatus: open -> closed

superseder: improper scope in list comprehension, when used in class declaration

nosy: + serhiy.storchaka
messages: + msg321362
resolution: duplicate
stage: resolved
2018年07月10日 05:36:11xtreaksetnosy: + xtreak
messages: + msg321355
2018年07月09日 15:57:52con-f-usecreate

AltStyle によって変換されたページ (->オリジナル) /