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


Groups > comp.lang.python > #29453

Re: User defined lexical scoping... can I do this?

Date 2012-09-18 23:51 +0200
From Thomas Jollans <t@jollybox.de>
Subject Re: User defined lexical scoping... can I do this?
References <d9aec95e-aedb-442d-ab6d-320f43f61b93@googlegroups.com> <f366b2f6-781a-4d66-9386-6be75f631ee3@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.892.1348005076.27098.python-list@python.org> (permalink)

Show all headers | View raw


On 09/18/2012 10:50 PM, weissman.mark@gmail.com wrote:
> Well there's wired stuff like this:
> 
> In [1]: locals()["x"] = 5
> 
> In [2]: print x
> 5
> 

No, there isn't. Modifying the dictionary returned by locals() has no
effect.

>>> def f ():
...     locals()["x"] = 1
...     return x
...
>>> f ()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in f
NameError: global name 'x' is not defined
>>>
>>> locals()["x"] = 1
>>> x
1
>>> #this works because
... locals() is globals()
True
>>>

The exception is the case when local scope is identical to global scope.
In this case, locals() has globals() semantics.

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


Thread

User defined lexical scoping... can I do this? porkfried <weissman.mark@gmail.com> - 2012-09-18 13:10 -0700
  Re: User defined lexical scoping... can I do this? Thomas Jollans <t@jollybox.de> - 2012-09-18 22:31 +0200
  Re: User defined lexical scoping... can I do this? weissman.mark@gmail.com - 2012-09-18 13:50 -0700
    Re: User defined lexical scoping... can I do this? Thomas Jollans <t@jollybox.de> - 2012-09-18 23:51 +0200
    Re: User defined lexical scoping... can I do this? Terry Reedy <tjreedy@udel.edu> - 2012-09-18 21:38 -0400
      Re: User defined lexical scoping... can I do this? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-19 04:03 +0000
  Re: User defined lexical scoping... can I do this? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-09-19 00:47 +0100

csiph-web