python eval function

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Aug 25 20:55:06 EDT 2013


On Sun, 25 Aug 2013 23:48:34 +0430, Mohsen Pahlevanzadeh wrote:

> Dear all,
> 
> eval doesn't run my code at the following link:
> http://stackoverflow.com/questions/18432198/eval-function-doesnt-work-
in-python


Are you asking a question or just sharing a useful link?



> Thank you before your answer...

What question do you want us to answer? The question on Stackoverflow has 
already been answered correctly: there is no need to use eval (or exec) 
for this basic task. 

Also, you're not writing C code. Get rid of the semi-colons, they are 
just a waste of time, and make you look like an incompetent programmer.

Using exec is not the right solution. That is unsafe, if the field name 
and content come from a database or external input, your code now has a 
*serious* Code Injection vulnerability. AND it is slow. So you have 
written slow, unsafe code. Get rid of it, and use setattr like you have 
already been told:

setattr(self, fieldName, fieldValue)



-- 
Steven



More information about the Python-list mailing list