Call Python eval Function

This example shows how to evaluate the expression x+y in Python®. To evaluate an expression, pass a Python dict value for the globals namespace parameter.

Read the help for eval.

py.help('eval')
Help on built-in function eval in module __builtin__:

eval(...)
    eval(source[, globals[, locals]]) -> value
    
    Evaluate the source in the context of globals and locals.
    The source may be a string representing a Python expression
    or a code object as returned by compile().
    The globals must be a dictionary and locals can be any mapping,
    defaulting to the current globals and locals.
    If only globals is given, locals defaults to it.

Create a Python dict variable for the x and y values.

workspace = py.dict(pyargs('x',1,'y',6))
workspace = 
  Python dict with no properties.

    {'y': 6.0, 'x': 1.0}

Evaluate the expression.

res = py.eval('x+y',workspace)
res = 7

Add two numbers without assigning variables. Pass an empty dict value for the globals parameter.

res = py.eval('1+6',py.dict)
res = int64
    7