I would also have classed 'eval' evaluating the global variables instead of the local variables of the same name as a bug.just a RTFM problem !
Code:
A = 1B = 2def Test(A, B, fn): print("A =", A) print("B =", B) print(A, "+", B, "=", A + B) print(fn, "=", eval(fn))Test(A=3, B=4, fn="A + B")Code:
pi@Pi4B:/tmp $ python e.pyA = 3B = 43 + 4 = 7A + B = 7<-- As expectedCode:
pi@Pi4B:/tmp $ mpremote run e.pyA = 3B = 43 + 4 = 7A + B = 3<-- Not as expected
Code running in eval() function doesn’t have access to local variables
Cause: MicroPython doesn’t maintain symbolic local environment, it is optimized to an array of slots. Thus, local variables can’t be accessed by a name. Effectively, eval(expr) in MicroPython is equivalent to eval(expr, globals(), globals()).
Statistics: Posted by hippy — Fri Dec 19, 2025 3:15 pm