Re: exec and globals and locals ...

2019-09-20 Thread jfong
Peter Otten於 2019年9月20日星期五 UTC+8下午3時31分48秒寫道: > jf...@ms4.hinet.net wrote: > > >>>> x = 3 > >>>> def foo(): > > ... exec("print(globals(), locals()); x = x + 1; print(globals(), > > locals())") ... > >>>> fo

Re: exec and globals and locals ...

2019-09-20 Thread Peter Otten
jf...@ms4.hinet.net wrote: >>>> x = 3 >>>> def foo(): > ... exec("print(globals(), locals()); x = x + 1; print(globals(), > locals())") ... >>>> foo() > {'foo': , '__package__': None, '__builtins__': > {, '__loader__': {'_froz

Re: exec and globals and locals ...

2019-09-19 Thread jfong
>>> x = 3 >>> def foo(): ... exec("print(globals(), locals()); x = x + 1; print(globals(), locals())") ... >>> foo() {'foo': , '__package__': None, '__builtins__': , '__loader__': , '__doc__': None, '__name__': '__main__', '__spec__': None, 'x'

Re: exec and globals and locals ...

2019-09-19 Thread Eko palypse
Am Donnerstag, 19. September 2019 20:24:49 UTC+2 schrieb Peter Otten: > Eko palypse wrote: > > > Am Donnerstag, 19. September 2019 18:31:43 UTC+2 schrieb Peter Otten: > >> Eko palypse wrote: > >> > >> > No, I have to correct myself > >> > > >> > x = 5 > >> > def f1(): > >> > exec("x = x +

Re: exec and globals and locals ...

2019-09-19 Thread Peter Otten
Eko palypse wrote: > Am Donnerstag, 19. September 2019 18:31:43 UTC+2 schrieb Peter Otten: >> Eko palypse wrote: >> >> > No, I have to correct myself >> > >> > x = 5 >> > def f1(): >> > exec("x = x + 1; print('f1 in:', x)") >> > return x >> > print('f1 out', f1()) >> > >> > results in

Re: exec and globals and locals ...

2019-09-19 Thread Eko palypse
Am Donnerstag, 19. September 2019 18:31:43 UTC+2 schrieb Peter Otten: > Eko palypse wrote: > > > No, I have to correct myself > > > > x = 5 > > def f1(): > > exec("x = x + 1; print('f1 in:', x)") > > return x > > print('f1 out', f1()) > > > > results in the same, for me confusing,

Re: exec and globals and locals ...

2019-09-19 Thread Eko palypse
First thank you for all the answers, very much appreciated. I assume the root cause might be explained by the zen of python as well. If the implementation is hard to explain, it's a bad idea. Maybe I need to rethink my implementation :-) Eren --

Re: exec and globals and locals ...

2019-09-19 Thread Peter Otten
Eko palypse wrote: > No, I have to correct myself > > x = 5 > def f1(): > exec("x = x + 1; print('f1 in:', x)") > return x > print('f1 out', f1()) > > results in the same, for me confusing, results. > > f1 in: 6 > f1 out 5 Inside a function exec assignments go to a *copy* of the local

Re: exec and globals and locals ...

2019-09-19 Thread Eko palypse
No, I have to correct myself x = 5 def f1(): exec("x = x + 1; print('f1 in:', x)") return x print('f1 out', f1()) results in the same, for me confusing, results. f1 in: 6 f1 out 5 Eren -- https://mail.python.org/mailman/listinfo/python-list

Re: exec and globals and locals ...

2019-09-19 Thread Peter Otten
Eko palypse wrote: > Thank you, I'm currently investigating importlib and read that > __builtins__ might be another alternative. > My ultimate goal would be to have objects available without the need to > import them, regardless whether used in a script directly or used in an > imported module.

Re: exec and globals and locals ...

2019-09-19 Thread Peter Otten
Richard Damon wrote: > On 9/19/19 6:16 AM, Eko palypse wrote: >>> In all cases, if the optional parts are omitted, the code is executed in >>> the current scope. ... >>> >>> >>> You can see from it that "globals" is optional. >>> And that, if "globals" is missing, then >>> "exec" is executed in

Re: exec and globals and locals ...

2019-09-19 Thread Bev In TX
I’m not the OP, but I want to thank you for that clarification. I had previously not understood the ramifications of the following in section “7. Simple statements” in “The Python Language Reference”: “An augmented assignment expression like x += 1 can be rewritten as x = x + 1 to achieve a

Re: exec and globals and locals ...

2019-09-19 Thread Richard Damon
On 9/19/19 6:52 AM, Eko palypse wrote: > Am Donnerstag, 19. September 2019 12:45:35 UTC+2 schrieb Richard Damon: >> On 9/19/19 6:16 AM, Eko palypse wrote: In all cases, if the optional parts are omitted, the code is executed in the current scope. ... You can see from it

Re: exec and globals and locals ...

2019-09-19 Thread Eko palypse
Am Donnerstag, 19. September 2019 12:56:59 UTC+2 schrieb Peter Otten: > Eko palypse wrote: > > >> Then it should be clear that the name 'test01' is put into globals(), if > >> load_module() doesn't throw an exception. No sharing or nesting of > >> namespaces takes place. > > > > Thank you too

Re: exec and globals and locals ...

2019-09-19 Thread Peter Otten
Eko palypse wrote: >> Then it should be clear that the name 'test01' is put into globals(), if >> load_module() doesn't throw an exception. No sharing or nesting of >> namespaces takes place. > > Thank you too for your answer. Ok, that means that in every case when exec > imports something it

Re: exec and globals and locals ...

2019-09-19 Thread Eko palypse
Am Donnerstag, 19. September 2019 12:45:35 UTC+2 schrieb Richard Damon: > On 9/19/19 6:16 AM, Eko palypse wrote: > >> In all cases, if the optional parts are omitted, the code is executed in > >> the current scope. ... > >> > >> > >> You can see from it that "globals" is optional. > >> And that,

Re: exec and globals and locals ...

2019-09-19 Thread Richard Damon
On 9/19/19 6:16 AM, Eko palypse wrote: >> In all cases, if the optional parts are omitted, the code is executed in the >> current scope. ... >> >> >> You can see from it that "globals" is optional. >> And that, if "globals" is missing, then >> "exec" is executed in the current scope ("f1" in your

Re: exec and globals and locals ...

2019-09-19 Thread Eko palypse
> Then it should be clear that the name 'test01' is put into globals(), if > load_module() doesn't throw an exception. No sharing or nesting of > namespaces takes place. Thank you too for your answer. Ok, that means that in every case when exec imports something it has its own global namespace,

Re: exec and globals and locals ...

2019-09-19 Thread Eko palypse
> In all cases, if the optional parts are omitted, the code is executed in the > current scope. ... > > > You can see from it that "globals" is optional. > And that, if "globals" is missing, then > "exec" is executed in the current scope ("f1" in your case). Thank you for your answer, and that

Re: exec and globals and locals ...

2019-09-19 Thread Peter Otten
Eko palypse wrote: > exec('import test01', globals()) > print('f3 out', x) > > # result exception, expected but because f1 didn't throw an exception > # I'm confused. module test01 has only this two lines > x += 1 > print('f3 in:', x) The lines above run in the test01's global namespace, not in

Re: exec and globals and locals ...

2019-09-18 Thread dieter
Eko palypse writes: > Why does f1 work? I've expected an exception as no global dict has been > provided > ... >def f1(...): > exec("...") >... The documentation ("https://docs.python.org/3/library/functions.html#exec;) tells you: exec(object[, glo

exec and globals and locals ...

2019-09-18 Thread Eko palypse
Why does f1 work? I've expected an exception as no global dict has been provided, and why does throw f3 an exception if it does, more or less, the same as f1? x += 5 def f1(): exec("x += 1; print('f1 in:', x)") return x print('f1 out', f1()) # result => f1 in: 6 # result => f1 out 5