fCONV_AUSRICHTG is not defined - Why?

MRAB python at mrabarnett.plus.com
Tue Nov 7 14:10:07 EST 2023


On 2023-11-07 18:30, dn via Python-list wrote:
> On 08/11/2023 06.47, Egon Frerich via Python-list wrote:
>> I've no idea why this happens. In a module there are lists and definitions:
> ...
> 
>>      ["%s%s%s " % (i[fCONV_AUSRICHTG], i[fLG2], i[fTYP]) for i in Felder])
>>    File "/home/egon/Entw/Geldspur/geldspur/gui/GUI_Konfig.py", line 90, 
>> in <listcomp>
>>      ["%s%s%s " % (i[fCONV_AUSRICHTG], i[fLG2], i[fTYP]) for i in Felder])
>> NameError: name 'fCONV_AUSRICHTG' is not defined
>> 
>> You see "Felder" and with "0 0 3 4" the correct value 4 for 
>> fCONV_AUSRICHTG. But there is the NameError.
>> 
>> What does <listcomp> mean? Is there a change from python2 to python3?
> 
> Works for me (Python 3.11 on Fedora-Linux 37)
> - both as a script, and simple/single import.
> 
> What happens when you extract the second dimension's definitions into a
> module of their own, and import that (with/out less-sophisticated join)?
> 
The missing detail is this line from the traceback:

    File "/home/egon/Entw/Geldspur/geldspur/gui/GUI_Konfig.py", line 11,
in <module>
      class GUIcfg:

Here's a small example that shows the problem:

----8<----
#!python3.11
# -*- encoding: utf-8 -*-

class Test:
     hello = "hello"
     print(hello)
     print([[zero] for _ in range(4)])
----8<----

and its traceback:

----8<----
hello
Traceback (most recent call last):
   File "C:\Projects\regex3\test_clipboard.py", line 4, in <module>
     class Test:
   File "C:\Projects\regex3\test_clipboard.py", line 7, in Test
     print([zero for _ in range(4)])
          ^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "C:\Projects\regex3\test_clipboard.py", line 7, in <listcomp>
     print([zero for _ in range(4)])
            ^^^^
NameError: name 'zero' is not defined
----8<----

'zero' is visible in:

     print(hello)

but not in:

     print([zero for _ in range(4)])

Something to do with how scoping is implemented in comprehensions?



More information about the Python-list mailing list