Transforming a str to an operator
dukeofperl at ml1.net
Fri Aug 28 16:18:48 CEST 2009
On Thu, 27 Aug 2009, r wrote:
> On Aug 27, 10:52 pm, Duke Normandin <dukeofp... at ml1.net> wrote:
> > How do I convert the contents of "op" from a string to an actual
> > arithmetic operator? eval() does not seem to be the answer. TIA!
> Try this..
> >>> op = '+'
> >>> one = '1'
> >>> two = '2'
> >>> one+op+two
> >>> eval(one+op+two)
> you could also use string formatting.
I see! Concatenate the strings within the "eval()" function. Of
course, it's prudent not to expose "eval" to the outside world. But
for learning purposes ....
Thanks for the input!
More information about the Python-list