Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #19760

Re: Question about name scope

Date 2012-02-01 15:41 -0800
From Ethan Furman <ethan@stoneleaf.us>
Subject Re: Question about name scope
References (3 earlier) <CALwzidmBvCmeMOiSjOscuiTx7VXVmMvWkBU7hwQW5JDV85+N4A@mail.gmail.com> <4F29BB9C.70405@stoneleaf.us> <CALwzid=qdawuq7qd2Qyj9xR1jUo-KLhYMKDwLxDHX06ZJ8aDOw@mail.gmail.com> <4F29C255.1050009@stoneleaf.us> <CALwzidkEL14pG3m3Pb=fQQin-YUTTwJ1PF4bCJaV3OYfwB2zTw@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.5339.1328142880.27778.python-list@python.org> (permalink)

Show all headers | View raw


Ian Kelly wrote:
> Sure, but that's not actually out of sync.  The argument of your exec
> evaluates to 'print (a)'.  You get two different results because
> you're actually printing two different variables.

Ah -- thanks, I missed that.


> You can get the dict temporarily out of sync:
> 
>>>> def f(x, y):
> ...     frob = None
> ...     loc = locals()
> ...     loc[x] = y
> ...     print(loc)
> ...     print(locals())
> ...     print(loc)
> ...
>>>> f('frob', 42)
> {'y': 42, 'x': 'frob', 'frob': 42, 'loc': {...}}
> {'y': 42, 'x': 'frob', 'frob': None, 'loc': {...}}
> {'y': 42, 'x': 'frob', 'frob': None, 'loc': {...}}
> 
> In this case, 'frob' is updated to 42 in the dict, but the optimized
> local is not updated.  Calling locals() again refreshes the dict.

I'm not sure what you mean by temporary:

--> def f(x, y):
...     frob = None
...     loc = locals()
...     loc[x] = y
...     print(loc)
...     print(locals())
...     print(loc)
...     print(locals())
...
-->
--> f('frob', 19)
{'y': 19, 'x': 'frob', 'frob': 19}
{'y': 19, 'x': 'frob', 'frob': None, 'loc': {...}}
{'y': 19, 'x': 'frob', 'frob': None, 'loc': {...}}
{'y': 19, 'x': 'frob', 'frob': None, 'loc': {...}}

Seems to be stuck that way.

Here is a better example I was thinking of:

--> def f(x, y):
...     locals()[x] = y
...     locals()['x'] = 17
...     print(locals())
...     print(x)
...     print(y)
...
--> f('a', 42)
{'y': 42, 'x': 'a', 'a': 42}
a
42

So locals() was updated with 'a', but not with the assignment to 'x'. 
And of course, if we tried to 'print(a)' we'd get a NameError.

~Ethan~

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Question about name scope Olive <diolu@bigfoot.com> - 2012-02-01 18:11 +0100
  Re: Question about name scope Rick Johnson <rantingrickjohnson@gmail.com> - 2012-02-01 09:21 -0800
  Re: Question about name scope Ethan Furman <ethan@stoneleaf.us> - 2012-02-01 09:43 -0800
  Re: Question about name scope Dave Angel <d@davea.name> - 2012-02-01 12:36 -0500
    Re: Question about name scope Mel Wilson <mwilson@the-wire.com> - 2012-02-01 13:47 -0500
      Re: Question about name scope Ian Kelly <ian.g.kelly@gmail.com> - 2012-02-01 14:49 -0700
      Re: Question about name scope Ian Kelly <ian.g.kelly@gmail.com> - 2012-02-01 15:38 -0700
      Re: Question about name scope Ethan Furman <ethan@stoneleaf.us> - 2012-02-01 14:24 -0800
      Re: Question about name scope Ian Kelly <ian.g.kelly@gmail.com> - 2012-02-01 16:00 -0700
      Re: Question about name scope Ethan Furman <ethan@stoneleaf.us> - 2012-02-01 15:08 -0800
      Re: Question about name scope Ian Kelly <ian.g.kelly@gmail.com> - 2012-02-01 16:47 -0700
      Re: Question about name scope Ethan Furman <ethan@stoneleaf.us> - 2012-02-01 14:53 -0800
        Re: Question about name scope Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-02-02 00:34 +0000
      Re: Question about name scope Ethan Furman <ethan@stoneleaf.us> - 2012-02-01 15:59 -0800
      Re: Question about name scope Ethan Furman <ethan@stoneleaf.us> - 2012-02-01 15:41 -0800
      Re: Question about name scope Ethan Furman <ethan@stoneleaf.us> - 2012-02-01 15:51 -0800
  Re: Question about name scope Chris Rebert <clp2@rebertia.com> - 2012-02-01 09:38 -0800
  Re: Question about name scope Christian Heimes <lists@cheimes.de> - 2012-02-01 18:50 +0100

csiph-web