Page 1 of 1

[SOLVED] exec() function doesnt return result?

Posted: Mon Mar 27, 2017 6:35 pm
by VladVons
the following code works on Python for PC and prints Result

Code: Select all

('Exec', 'Result = (2+3)*2', 'Result', 10)
But doesnt work on MicroPython ESP8266
NameError: name 'Result' is not defined

exec() listed in builtin MicroPython function, with no description or usage
http://docs.micropython.org/en/latest/w ... ltins.html

Any ideas?

Code: Select all

def ExecScript(aValue = "Result = (2+3)*2"):
    exec(aValue)
    print("Exec", aValue, "Result", Result)
    return Result

Re: exec() function doesnt work?

Posted: Mon Mar 27, 2017 7:47 pm
by stijn
IIRC with uPy's exec you have to pass the other 2 arguments as well, e.g.

Code: Select all

loc = {}
exec('a=1', globals(), loc)
print(loc.a)

Re: exec() function doesnt work?

Posted: Mon Mar 27, 2017 8:53 pm
by VladVons
Doesnt work. Error:

Code: Select all

AttributeError: 'dict' object has no attribute 'a'

Re: exec() function doesnt work?

Posted: Mon Mar 27, 2017 8:58 pm
by dhylands
Using loc['a'] will give better results.

Re: exec() function doesnt work?

Posted: Mon Mar 27, 2017 9:36 pm
by VladVons
thanks. works!

Code: Select all

def Exec(aValue = "Result = (2+3)*2"):
    Vars = {}
    try:
        exec(aValue, globals(), Vars)
        Result = Vars.get('Result')
    except Exception as E:
        Result = E
    return Result

Re: [SOLVED] exec() function doesnt return result?

Posted: Tue Mar 28, 2017 7:29 am
by stijn
Using loc['a'] will give better results.
thanks!

Note that there's also built-in eval which for the case shown is simpler and less error-prone (by not forcing the statement passed in to have a Result variable bu instead just taking an expression):

Code: Select all

def Exec(expression='(2+3)*2'):
    try:
        return eval(expression)
    except Exception as e:
        return e
Note that returning expressions as a way of error-handling is rather non-pythonic and puts an extra burden on the caller of the function as now the return value has to be checked (can be None, a value, or an Exception) and makes it hard to distinguish between actual errors and e.g. Exec('Exception()')

Re: [SOLVED] exec() function doesnt return result?

Posted: Tue Mar 28, 2017 7:40 am
by pythoncoder
stijn wrote:...returning expressions as a way of error-handling is rather non-pythonic...
Indeed. I'd suggest having the caller do the exception handling.

Re: [SOLVED] exec() function doesnt return result?

Posted: Tue Mar 28, 2017 10:00 pm
by VladVons
eval calculates epressions only.

it cant call and execute methods or functions

Code: Select all

eval("print('Hello World')")
SyntaxError: invalid syntax

Re: [SOLVED] exec() function doesnt return result?

Posted: Wed Mar 29, 2017 7:31 am
by stijn
That is not the correct definition of the difference between eval and exec. See e.g. http://stackoverflow.com/questions/2220 ... -in-python
Eval can call and exectude methods and functions:

Code: Select all

>>> def foo():
        return True
>>> eval('foo()')
True
At first I thought your example might not work because of escaping quotes but I realized that makes no sense.
And indeed it does actually work for me:

Code: Select all

MicroPython v1.8.7-475-g406ff2f on 2017-03-23; win32 version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> eval("print('Hello World')")
Hello World
>>>
I assume you are testing this on CPython 2 or so, that's not a good idea as differences with Python 3 and hence uPy are sometimes major.