The below code snippet is getting different outputs from Python 2.7 and 3.3.
data = {'_out':[1,2,3,3,4]}
codes = ['_tmp=[]',
'[_tmp.append(x) for x in _out if x not in _tmp]',
'print(_tmp)']
for c in codes:
exec(c,{},data)
Output from Python 2.7:
[1,2,3,4]
Output from Python 3.3:
Traceback (most recent call last):
File "test.py", line 8, in <module>
exec(c,{},data)
File "<string>", line 1, in <module>
File "<string>", line 1, in <listcomp>
NameError: global name '_tmp' is not defined
To fix the error in Python 3.3, I simply set the globals to be as the same as locals, that is exec(c,data,data)
. Any idea why Python 3.3 is not behaving as that in 2.7?