Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #24858 > unrolled thread
| Started by | eisoab@gmail.com |
|---|---|
| First post | 2012-07-04 04:56 -0700 |
| Last post | 2012-07-04 22:57 +0000 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.lang.python
locals().update(...) eisoab@gmail.com - 2012-07-04 04:56 -0700
Re: locals().update(...) Christian Heimes <lists@cheimes.de> - 2012-07-04 14:14 +0200
Re: locals().update(...) Dave Angel <d@davea.name> - 2012-07-04 18:48 -0400
Re: locals().update(...) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-07-04 22:57 +0000
| From | eisoab@gmail.com |
|---|---|
| Date | 2012-07-04 04:56 -0700 |
| Subject | locals().update(...) |
| Message-ID | <451cfda5-3fe6-4066-94f3-31f4fe747233@googlegroups.com> |
I expected this to work:
def f(**args):
locals().update(args)
print locals()
print a
d=dict(a=1)
f(**d)
but:
> global name 'a' is not defined
Where is my mistake?
This does work:
globals().update({'a':1})
print a
1
-E
[toc] | [next] | [standalone]
| From | Christian Heimes <lists@cheimes.de> |
|---|---|
| Date | 2012-07-04 14:14 +0200 |
| Message-ID | <mailman.1788.1341404099.4697.python-list@python.org> |
| In reply to | #24858 |
Am 04.07.2012 13:56, schrieb eisoab@gmail.com: > I expected this to work: It doesn't work and that's documented: http://docs.python.org/library/functions.html?highlight=locals#locals
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <d@davea.name> |
|---|---|
| Date | 2012-07-04 18:48 -0400 |
| Message-ID | <mailman.1800.1341442122.4697.python-list@python.org> |
| In reply to | #24858 |
On 07/04/2012 07:56 AM, eisoab@gmail.com wrote:
> I expected this to work:
>
>
> def f(**args):
> locals().update(args)
> print locals()
> print a
>
> d=dict(a=1)
>
> f(**d)
>
> but:
>> global name 'a' is not defined
> Where is my mistake?
Chris has given you the place where it's documented that it generally
won't work. I'll try to explain why, at least for local variables
inside functions, and for CPython implementation.
The generated byte code for a function does *not* look up each symbol by
name during execution. The compiler builds a list of known local
symbol names, and gives them each an index (a small positive integer).
At run time that list is fixed in size and meaning; no new locals can
be added. The locals() function just creates a dictionary that
approximates what the semantics of the symbols are. But changes to that
dictionary have no effect on the running function.
That's one reason that copying a global value to a local symbol inside a
function can make that function run faster. Globals always require a
dictionary lookup, while function locals are just an integer offset away.
> This does work:
>
> globals().update({'a':1})
>
> print a
>
> 1
>
> -E
>
As i said above, the rules are different for globals, and they are even
if you use locals() to access them. However, i'd consider it prudent
never to assume you can write to the dictionary constructed by either
the locals() or the globals() functions.
--
DaveA
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2012-07-04 22:57 +0000 |
| Message-ID | <4ff4ca5c$0$29988$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #24878 |
On Wed, 04 Jul 2012 18:48:08 -0400, Dave Angel wrote: > As i said above, the rules are different for globals, and they are even > if you use locals() to access them. However, i'd consider it prudent > never to assume you can write to the dictionary constructed by either > the locals() or the globals() functions. globals() is implicitly documented as being writable: "This is always the dictionary of the current module..." -- unless you have a module with a read-only dict, that is writable. http://docs.python.org/release/3.2/library/functions.html#globals There are tricks to getting read-only namespaces, but you can legitimately expect to write to globals(). -- Steven
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web