Re: Why are both locals() and globals() set?

2017-12-23 Thread MRAB
On 2017-12-23 04:01, Peng Yu wrote: Hi, The following example shows that both locals() and globals() are updated when x and f are defined. Shouldn't they be considered and global variable and functions only? Why does it make sense to set locals() as well? Thanks. It's "local&quo

Re: Why are both locals() and globals() set?

2017-12-22 Thread Steve D'Aprano
On Sat, 23 Dec 2017 03:01 pm, Peng Yu wrote: > Hi, The following example shows that both locals() and globals() are > updated when x and f are defined. Shouldn't they be considered and > global variable and functions only? Why does it make sense to set > locals() as well? Thanks.

Why are both locals() and globals() set?

2017-12-22 Thread Peng Yu
Hi, The following example shows that both locals() and globals() are updated when x and f are defined. Shouldn't they be considered and global variable and functions only? Why does it make sense to set locals() as well? Thanks. $ cat ./main.py #!/usr/bin/env python # vim: set noexpandtab ta

Re: locals() and globals()

2006-10-14 Thread Kay Schluehr
Paolo Pantaleo wrote: > Hi > > this exaple: > > def lcl(): > n=1 > x=locals() > x["n"]=100 > print "n in lcl() is:" +str(n) > #This will say Name error > #x["new"]=1 > #print new > > > n=1 > x=globals() > x["n"]=100 > print "gobal n is:" +str(n) > x["new"]=1 > print "new

Re: locals() and globals()

2006-10-14 Thread Fredrik Lundh
Paolo Pantaleo wrote: > why accessing the names dictionary globals() and locals() gives > different results? the documentation has the answer: "The contents of this dictionary should not be modified; changes may not affect the values of local variables used by the interpreter" http:

locals() and globals()

2006-10-14 Thread Paolo Pantaleo
Hi this exaple: def lcl(): n=1 x=locals() x["n"]=100 print "n in lcl() is:" +str(n) #This will say Name error #x["new"]=1 #print new n=1 x=globals() x["n"]=100 print "gobal n is:" +str(n) x["new"]=1 print "new is:" +str(new) lcl() produces gobal n is:100 new is:1 n

Re: updating locals() and globals() (WAS: How do I do this? (eval() on the left hand side))

2004-12-09 Thread Bengt Richter
On Wed, 8 Dec 2004 20:22:52 -0500, "Terry Reedy" <[EMAIL PROTECTED]> wrote: >To respond to and summarize several posts in this discussion: > >Within a function, where the local namespace is distinct from the global >(module) namespace, CPython usually implements the local namespace >internally a

Re: exec'ing functions (WAS: updating locals() and globals())

2004-12-08 Thread Steven Bethard
Jeff Shannon wrote: Steven Bethard wrote: Jeff Shannon wrote: Note also that functions which use exec cannot use the static namespace optimization, and thus tend to be *much* slower than normal functions In what circumstances will this be true? I couldn't verify it: [snip] I was referring to fu

Re: updating locals() and globals() (WAS: How do I do this? (eval() on the left hand side))

2004-12-08 Thread Terry Reedy
To respond to and summarize several posts in this discussion: Within a function, where the local namespace is distinct from the global (module) namespace, CPython usually implements the local namespace internally as a fixed-length array. When this is true, locals() is a *copy* of the local nam

Re: exec'ing functions (WAS: updating locals() and globals())

2004-12-08 Thread Jeff Shannon
Steven Bethard wrote: Jeff Shannon wrote: Note also that functions which use exec cannot use the static namespace optimization, and thus tend to be *much* slower than normal functions In what circumstances will this be true? I couldn't verify it: [...] exec """\ def fib2(n): a, b = 0, 1

exec'ing functions (WAS: updating locals() and globals())

2004-12-08 Thread Steven Bethard
Jeff Shannon wrote: Note also that functions which use exec cannot use the static namespace optimization, and thus tend to be *much* slower than normal functions In what circumstances will this be true? I couldn't verify it: > cat fib.py def fib1(n): a, b = 0, 1 while True: a, b

Re: updating locals() and globals() (WAS: How do I do this? (eval() on the left hand side))

2004-12-08 Thread Steven Bethard
Jeff Shannon wrote: (Note also that functions which use exec cannot use the static namespace optimization, and thus tend to be *much* slower than normal functions (in addition to being a huge security problem). I don't know, however, whether locals() can update the local namespace in such un-op

Re: updating locals() and globals() (WAS: How do I do this? (eval() on the left hand side))

2004-12-08 Thread Jeff Shannon
Steven Bethard wrote: I remember hearing an explanation of why locals() is not writable that had to do with something about efficiency and the call stack (or something like that)... IIRC, the function local namespace (in CPython) is kept internally as a static array, rather than as a dictionary

Re: updating locals() and globals() (WAS: How do I do this? (eval() on the left hand side))

2004-12-08 Thread Steven Bethard
t updated. locals() is readable but not writable in any case where locals() is not globals(), I believe. But "unreliable" sounds kinda vague and intermittent, and I assume that is not the case here - What is the Rule(tm)? Unfortunately, I don't think there is a Rule(tm) because t

Re: updating locals() and globals() (WAS: How do I do this? (eval() on the left hand side))

2004-12-08 Thread Caleb Hattingh
- What is the Rule(tm)? Thanks Caleb On Wed, 08 Dec 2004 16:59:23 GMT, Steven Bethard <[EMAIL PROTECTED]> wrote: Peter Hansen wrote: Nick Coghlan wrote: Generally, altering the contents of the dicts returned by locals() and globals() is unreliable at best. Nick, could you please comme

updating locals() and globals() (WAS: How do I do this? (eval() on the left hand side))

2004-12-08 Thread Steven Bethard
Peter Hansen wrote: Nick Coghlan wrote: Generally, altering the contents of the dicts returned by locals() and globals() is unreliable at best. Nick, could you please comment on why you say this about globals()? I've never heard of any possibility of "unreliability" in updating gl