Python 3 eval() difference with list comprehension
Closed this issue · 1 comments
road-cycling commented
https://bugs.python.org/issue5242
class Test:
def __init__(self):
self.item = 'item'
statement = """[item for item in ['this', 'test'] if item == self.item]"""
print(eval(statement))
Test()
Causes the following error.
print(eval(statement))
File "<string>", line 1, in <module>
File "<string>", line 1, in <listcomp>
NameError: name 'self' is not defined
The change python3 made to the eval function causes an issue with certain plugins that reference theself
object inside of a list comprehension.
The fix is to bring the self
object in as local scope.
From The Above Example print(eval(statement, {'self':self}))
Internal Codebase
transform = eval(target_map[u'transform'], {'self': self})
eval(parsed_expression, {'self': self, 'index': index})
DaniBoie commented
Thank you Git Gods.