eval und lambda
Verfasst: Sonntag 30. September 2012, 18:26
Hallo,
ich bin über eine Sache gestolpert, von der ich nicht weiß, warum sich Python so verhält:
Ich hätte erwartet, dass sich beide Befehle gleich verhalten. Wieso ist das nicht der Fall?
al
ich bin über eine Sache gestolpert, von der ich nicht weiß, warum sich Python so verhält:
Code: Alles auswählen
>>> z=lambda x: x>0 and x or 0.; sum([z(i) for i in [-100,0,100,200]])
300.0
>>> eval("z=lambda x: x>0 and x or 0.; sum([z(i) for i in [-100,0,100,200]])")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1
z=lambda x: x>0 and x or 0.; sum([z(i) for i in [-100,0,100,200]])
^
SyntaxError: invalid syntaxal