Wednesday 15 February 2012

python - eval() does not assign variable at runtime -


I use eval () to specify a list in var:

  eval ('mylist = [1,2,3]')   

But when I run it, I get a syntax error. what's wrong with it? If I can not assign an assignment in eval () , how do I specify a var in sequence?

Use exec for the statement:

  & gt; & Gt; & Gt; Exec 'lis = [1,2,3]' & gt; & Gt; & Gt; LIS [1, 2, 3]   

eval works on expressions only, such as 2 * 2 , 4 + 5 etc.

eval and exec is fine if the string is coming from a known source, but do not use them if String is coming from an unknown source (user input).

Read:

No comments:

Post a Comment