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: eval() function in List Comprehension doesn't work
Type: Stage:
Components: Versions: Python 3.0
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: JiafeiPeng, QuantumTim, ezio.melotti, georg.brandl, hagen
Priority: normal Keywords:

Created on 2009-02-13 09:42 by JiafeiPeng, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
unnamed JiafeiPeng, 2009-02-13 10:07
unnamed JiafeiPeng, 2009-02-13 10:14
Messages (9)
msg81890 - (view) Author: Jiafei Peng (JiafeiPeng) Date: 2009-02-13 09:42
eval() function in List Comprehension doesn't work.
please see the under codes:

canBusType = 'CANdiag'
result = [eval('canBusType') for i in range(3)]
NameError: name 'canBusType' is not defined

It did work in Python2.5 or 2.6. The expected result is 
['CANdiag', 'CANdiag', 'CANdiag'].
msg81891 - (view) Author: Hagen Fürstenau (hagen) Date: 2009-02-13 09:54
I can't reproduce this. For me it works as expected.
msg81895 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2009-02-13 10:04
I can't reproduce it either, tested with Py3 (on Linux and Windows) and
with Py2.[456], it worked fine everywhere.

Does your eval() work properly outside listcomps?
msg81896 - (view) Author: Jiafei Peng (JiafeiPeng) Date: 2009-02-13 10:07
Hallo Mr. Fürstenau,

thank you for the quick response.
Have you tried it with Python 3.0

The Error report is truely:
NameError: name 'canBusType' is not defined

Someone else reported the same problem in internet too.

Best regards, mit freundlichen Grüßen,

Jiafei Peng

Softwareentwickler / Embedded System Software (EF-F2)
Software developer / Embedded System Software 

IAV GmbH
Nordhoffstr. 5
38518 Gifhorn
GERMANY

Phone: +49 5371  805-2817
Fax:+49 5371  805-1330

E-mail:  <mailto:Jiafei.Peng@iav.de>
Internet: http://www.iav.de

IAV GmbH
Sitz/Registered Office: Berlin
Registergericht/Registration Court: Amtsgericht Charlottenburg
Registernummer/Company Registration Number: HRB 21 280
Geschäftsführer/Managing Directors: Kurt Blumenröder, Michael Schubert

Hagen Fürstenau <report@bugs.python.org> 
13.02.2009 10:54
Bitte antworten an
Python tracker <report@bugs.python.org>

An
jiafei.peng@iav.de
Kopie

Thema
[issue5242] eval() function in List Comprehension doesn't work 

Hagen Fürstenau <hfuerstenau@gmx.net> added the comment:

I can't reproduce this. For me it works as expected.

----------
nosy: +hagen

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue5242>
_______________________________________
msg81897 - (view) Author: Jiafei Peng (JiafeiPeng) Date: 2009-02-13 10:14
Yes
it does work properly outside listcomps.

            canBusType = 'CANdiag'
            result1 = eval('canBusType')
            result2 = [eval('canBusType'), eval('canBusType'), eval(
'canBusType')]
            result3 = [eval('canBusType') for i in range(3)]

result1 = 'CANdiag'
result2 =['CANdiag' 'CANdiag' 'CANdiag']
for result3:
NameError: name 'canBusType' is not defined

Best regards, mit freundlichen Grüßen,

Jiafei Peng

Softwareentwickler / Embedded System Software (EF-F2)
Software developer / Embedded System Software 

IAV GmbH
Nordhoffstr. 5
38518 Gifhorn
GERMANY

Phone: +49 5371  805-2817
Fax:+49 5371  805-1330

E-mail:  <mailto:Jiafei.Peng@iav.de>
Internet: http://www.iav.de

IAV GmbH
Sitz/Registered Office: Berlin
Registergericht/Registration Court: Amtsgericht Charlottenburg
Registernummer/Company Registration Number: HRB 21 280
Geschäftsführer/Managing Directors: Kurt Blumenröder, Michael Schubert

Ezio Melotti <report@bugs.python.org> 
13.02.2009 11:04
Bitte antworten an
Python tracker <report@bugs.python.org>

An
jiafei.peng@iav.de
Kopie

Thema
[issue5242] eval() function in List Comprehension doesn't work 

Ezio Melotti <ezio.melotti@gmail.com> added the comment:

I can't reproduce it either, tested with Py3 (on Linux and Windows) and
with Py2.[456], it worked fine everywhere.

Does your eval() work properly outside listcomps?

----------
nosy: +ezio.melotti

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue5242>
_______________________________________
msg81898 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2009-02-13 10:16
Ezio: this happens inside a function, like this:

def f():
    canBusType = 'CANdiag'
    result = [eval('canBusType') for i in range(3)]

This is expected, and won't easily fix.  The reason is that list
comprehensions in 3.x use a function namespace "under the hood" (in 2.x,
they were implemented like a simple for loop). Because inner functions
need to know what names to get from what enclosing namespace, the names
referenced in eval() can't come from enclosing functions. They must
either be locals or globals.

Of course, the question to the OP is why eval() is needed anyway.
msg81918 - (view) Author: Tim Gordon (QuantumTim) Date: 2009-02-13 13:01
If you know what variable you are going to be eval-ing, or at least, 
have a list of those that might be eval-ed, you can get around this 
issue by making sure they are explicitly referenced in the inner scope 
(i.e., in the list comprehension).  For example, even though list 
comprehensions work in 2.x, generator expressions don't, but this hack 
does (on 2.4 at least):

def f():
  canBusType = 'CANdiag'
  return (eval('canBusType') for i in range(3) if True or canBusType)

By putting a semantically vacuous reference to canBusType (and any 
other variables you want) you make sure they are usable from within the 
eval as well.
msg81926 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2009-02-13 13:44
eval() is probably already an hack, there's no need to add another hack
to make it work. It's better to just get rid of eval() and find a better
way to do what you want to do.
msg82033 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2009-02-14 12:36
I agree.
History
Date User Action Args
2022-04-11 14:56:45adminsetgithub: 49492
2021-11-22 08:35:59mark.dickinsonlinkissue45862 superseder
2020-07-06 12:51:13eric.smithlinkissue41216 superseder
2009-02-14 12:36:08georg.brandlsetstatus: pending -> closed
messages: + msg82033
2009-02-13 13:44:39ezio.melottisetmessages: + msg81926
2009-02-13 13:01:04QuantumTimsetnosy: + QuantumTim
messages: + msg81918
2009-02-13 10:42:06georg.brandllinkissue5244 superseder
2009-02-13 10:16:03georg.brandlsetstatus: open -> pending
nosy: + georg.brandl
resolution: wont fix
messages: + msg81898
2009-02-13 10:14:38JiafeiPengsetfiles: + unnamed
messages: + msg81897
2009-02-13 10:07:32JiafeiPengsetfiles: + unnamed
messages: + msg81896
2009-02-13 10:04:25ezio.melottisetnosy: + ezio.melotti
messages: + msg81895
2009-02-13 09:54:11hagensetnosy: + hagen
messages: + msg81891
2009-02-13 09:42:04JiafeiPengcreate