Suggestion for "syntax error": ++i, --i
Steven Bethard
steven.bethard at gmail.com
Mon Dec 13 13:50:38 EST 2004
Peter Maas wrote:
>
> target = 'a='
> sign = '-'
> operand = '-2'
>
> exec(target+sign+operand)
Hopefully not too many people write code like above when it isn't much
harder to write this without exec:
>>> target = 'a'
>>> sign = '-'
>>> operand = '-2'
>>> sign_functions = {'+':operator.pos, '-':operator.neg}
>>> globals()[target] = sign_functions[sign](int(operand))
>>> a
2
This avoids fun with exec like:
target = 'a='
sign = '-'
operand = '-2; import os; os.removedirs("/")'
=)
Steve
More information about the Python-list
mailing list