Вызывание Функции eval PythonЭтот пример показывает, как оценить выражение x+y в Python®. Чтобы оценить выражение, передайте значение dict Python для параметра пространства имен globals.
Считайте справку для eval.
py.help('eval')
Help on built-in function eval in module builtins:
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.
Создайте переменную dict Python для значений y и x.
workspace = py.dict(pyargs('x',1,'y',6))
workspace =
Python dict with no properties.
{'y': 6.0, 'x': 1.0}
Оцените выражение.
res = py.eval('x+y',workspace)
res =
7
Добавьте два числа, не присваивая переменные. Передайте пустое значение dict для параметра globals.
res = py.eval('1+6',py.dict)
res =
Python int with properties:
denominator: [1×1 py.int]
imag: [1×1 py.int]
numerator: [1×1 py.int]
real: [1×1 py.int]
7