say, I have very simple piece of code
py = """ a = 1 print (f'before all {a=}') def bar(n): print(f'bye {n=}') def foo(n): print(f'hello {n=}') bar(n) bar ('just works') foo ('sara') """ loc = {} glo = {} bytecode = compile(py, "script", "exec") exec(bytecode, glo, loc) as you can see I defined two functions: bar & foo and called both of them with results:
before all a=1 bye n='just works' hello n='sara' Traceback (most recent call last): File "/home/bla-bla/pythonProject/main_deco2.py", line 43, in <module> exec(bytecode, glo, loc) File "script", line 13, in <module> File "script", line 10, in foo NameError: name 'bar' is not defined and this leaves me puzzled, as I don't understand why function foo doesn't see bar when just a second ago I was able to call bar without a problem ?
fooparts in the code, and examine the dicts afterwards, you will find thataandbarend up in the locals dict, not the globals dict.