Simple and safe evaluator

bvdp bob at
Wed Jun 11 22:25:32 CEST 2008

Is there a simple/safe expression evaluator I can use in a python 
program. I just want to pass along a string in the form "1 + 44 / 3" or 
perhaps "1 + (-4.3*5)" and get a numeric result.

I can do this with eval() but I really don't want to subject my users to 
the problems with that method.

In this use I don't need python to worry about complex numbers, 
variables or anything else. Just do the math on a set of values. Would 
eval() with some restricted list of permitted operators do the trick?

I'm feeling too lazy to write/debug my own parser for this :)

Thanks, Bob.

More information about the Python-list mailing list