First possible raw solution:

from operator import add, sub, mul, div, neg

def evaluate(expr):
    if isinstance(expr, list):
        fun, ops = expr[0], expr[1:]
        return fun(*map(evaluate, ops))
    else:
        return expr

example = [add, [add, [sub, 5, 4], [mul, 3, 2]], [neg, 5]]
print evaluate(example)

But it's rather slow...

Bye,
bearophile

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to