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
eval() function in List Comprehension doesn't work #49492
Comments
eval() function in List Comprehension doesn't work. 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 |
I can't reproduce this. For me it works as expected. |
I can't reproduce it either, tested with Py3 (on Linux and Windows) and Does your eval() work properly outside listcomps? |
Hallo Mr. Fürstenau, thank you for the quick response. The Error report is truely: Someone else reported the same problem in internet too. Best regards, mit freundlichen Grüßen, Jiafei Peng Softwareentwickler / Embedded System Software (EF-F2) IAV GmbH Phone: +49 5371 805-2817 E-mail: <mailto:Jiafei.Peng@iav.de> IAV GmbH Hagen Fürstenau <report@bugs.python.org> An Thema Hagen Fürstenau <hfuerstenau@gmx.net> added the comment: I can't reproduce this. For me it works as expected. ---------- Python tracker <report@bugs.python.org> |
Yes 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) IAV GmbH Phone: +49 5371 805-2817 E-mail: <mailto:Jiafei.Peng@iav.de> IAV GmbH Ezio Melotti <report@bugs.python.org> An Thema Ezio Melotti <ezio.melotti@gmail.com> added the comment: I can't reproduce it either, tested with Py3 (on Linux and Windows) and Does your eval() work properly outside listcomps? ---------- Python tracker <report@bugs.python.org> |
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 Of course, the question to the OP is why eval() is needed anyway. |
If you know what variable you are going to be eval-ing, or 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 |
eval() is probably already an hack, there's no need to add another hack |
I agree. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: