[SOLVED] exec() function doesnt return result?

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
VladVons
Posts: 60
Joined: Sun Feb 12, 2017 6:49 pm
Location: Ukraine

[SOLVED] exec() function doesnt return result?

Post by VladVons » Mon Mar 27, 2017 6:35 pm

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
Last edited by VladVons on Mon Mar 27, 2017 9:37 pm, edited 3 times in total.

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: exec() function doesnt work?

Post by stijn » Mon Mar 27, 2017 7:47 pm

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)

VladVons
Posts: 60
Joined: Sun Feb 12, 2017 6:49 pm
Location: Ukraine

Re: exec() function doesnt work?

Post by VladVons » Mon Mar 27, 2017 8:53 pm

Doesnt work. Error:

Code: Select all

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

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: exec() function doesnt work?

Post by dhylands » Mon Mar 27, 2017 8:58 pm

Using loc['a'] will give better results.

VladVons
Posts: 60
Joined: Sun Feb 12, 2017 6:49 pm
Location: Ukraine

Re: exec() function doesnt work?

Post by VladVons » Mon Mar 27, 2017 9:36 pm

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

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

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

Post by stijn » Tue Mar 28, 2017 7:29 am

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()')

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

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

Post by pythoncoder » Tue Mar 28, 2017 7:40 am

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.
Peter Hinch
Index to my micropython libraries.

VladVons
Posts: 60
Joined: Sun Feb 12, 2017 6:49 pm
Location: Ukraine

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

Post by VladVons » Tue Mar 28, 2017 10:00 pm

eval calculates epressions only.

it cant call and execute methods or functions

Code: Select all

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

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

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

Post by stijn » Wed Mar 29, 2017 7:31 am

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.

Post Reply