Skip to content Skip to sidebar Skip to footer

How Do I Lazy Evaluate Variables In A Python Eval Expression

The scenario is that my user can supply an expression string for evaluation. It could be: 'power=(x**2+y**2)**0.5' Then I get an input stream of data with labels. E.g.: x ; y ; z

Solution 1:

The best solution to the question is also posted in the linked question:

import ast
defvarsInExpression(expr):
    st = ast.parse(expr)
    returnset(node.idfor node in ast.walk(st) iftype(node) is ast.Name)

This was posted by André Laszlo

It allows me to initialize the needed vars and functions before receiving any data and only precalculate "smart" variables that are used

The lazy evaluation part has not yet received a good answer

Post a Comment for "How Do I Lazy Evaluate Variables In A Python Eval Expression"