Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > User defined lexical scoping... can I do this?

Reply
Thread Tools

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

 
 
porkfried
Guest
Posts: n/a
 
      09-18-2012
I want to define a 'with' command that makes entries
in dictionary available within the local scope, and
stores new local variables into that dictionary. The
original scope should be restored on exit, and called
functions should not see anything special. Can I do this?

my_dict = dict(a=1, b=2)
with MyScope(my_dict):
print "A", a, "B", b
x = 3
print my_dict["x"]
print x # FAIL, unbound
 
Reply With Quote
 
 
 
 
Thomas Jollans
Guest
Posts: n/a
 
      09-18-2012
On 09/18/2012 10:10 PM, porkfried wrote:
> I want to define a 'with' command that makes entries
> in dictionary available within the local scope, and
> stores new local variables into that dictionary. The
> original scope should be restored on exit, and called
> functions should not see anything special. Can I do this?


No.*

It is not possible to set locals by ways other than an assignment**, and
it is certainly not possible to set locals in a scope other than the one
you're in**.

You should simply type out the dict's name. This is a lot clearer. If
you want to minimize typing, you can give the variable a one-character name.

Also, Python scope simply doesn't work like that. There is no block
scope, only local (=function) scope, and global scope, with a dash of
non-locals to spice things up a bit.

>
> my_dict = dict(a=1, b=2)
> with MyScope(my_dict):
> print "A", a, "B", b
> x = 3
> print my_dict["x"]
> print x # FAIL, unbound
>


*You could set global variables, and remove them on exit, but this is
ugly for a number of reasons: this could overwrite existing globals if
you're not very careful, called functions would see these globals, and
they would also be exposed to other threads.

**I believe there is actually a way to edit a caller's locals, but this
is not documented, not portable across Python implementations and
versions, and you couldn't create new locals like this, so it'd be
fairly pointless here


 
Reply With Quote
 
 
 
 
weissman.mark@gmail.com
Guest
Posts: n/a
 
      09-18-2012
On Tuesday, September 18, 2012 4:10:32 PM UTC-4, porkfried wrote:
> I want to define a 'with' command that makes entries
>
> in dictionary available within the local scope, and
>
> stores new local variables into that dictionary. The
>
> original scope should be restored on exit, and called
>
> functions should not see anything special. Can I do this?
>
>
>
> my_dict = dict(a=1, b=2)
>
> with MyScope(my_dict):
>
> print "A", a, "B", b
>
> x = 3
>
> print my_dict["x"]
>
> print x # FAIL, unbound


Well there's wired stuff like this:

In [1]: locals()["x"] = 5

In [2]: print x
5

In [3]:

but it didn't help me do what I wanted.
 
Reply With Quote
 
Thomas Jollans
Guest
Posts: n/a
 
      09-18-2012
On 09/18/2012 10:50 PM, http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
Mark Lawrence
Guest
Posts: n/a
 
      09-18-2012
On 18/09/2012 21:10, porkfried wrote:
> I want to define a 'with' command that makes entries
> in dictionary available within the local scope, and
> stores new local variables into that dictionary. The
> original scope should be restored on exit, and called
> functions should not see anything special. Can I do this?
>
> my_dict = dict(a=1, b=2)
> with MyScope(my_dict):
> print "A", a, "B", b
> x = 3
> print my_dict["x"]
> print x # FAIL, unbound
>


If you could state what you're trying to achieve rather than how you're
trying to achieve it then perhaps people could give you a solution to
your problem.

--
Cheers.

Mark Lawrence.

 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      09-19-2012
On 9/18/2012 5:51 PM, Thomas Jollans wrote:
> On 09/18/2012 10:50 PM, (E-Mail Removed) 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.


Last time I tried it, it does within a class -- in cpython at least.
That locals dict usually becomes the __dict__ of the class. But not to
be depended on indefinitely and across implmentations.


--
Terry Jan Reedy

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      09-19-2012
On Tue, 18 Sep 2012 21:38:19 -0400, Terry Reedy wrote:

> On 9/18/2012 5:51 PM, Thomas Jollans wrote:
>> On 09/18/2012 10:50 PM, (E-Mail Removed) 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.

>
> Last time I tried it, it does within a class -- in cpython at least.
> That locals dict usually becomes the __dict__ of the class. But not to
> be depended on indefinitely and across implmentations.


Exactly. The behaviour of modifying the dict returned by locals() is not
defined. For example, this is what happens under Python 2.6, Jython 2.5,
and IronPython 2.6:

steve@runes:~$ cat test.py

a = b = 'global'
def test():
a = None
locals()['a'] = 'local'
locals()['b'] = 'local'
print a, b

test()

steve@runes:~$ python test.py
None global
steve@runes:~$ jython test.py
None global
steve@runes:~$ ipy test.py
local global


Other Python implementations may do differently.



--
Steven
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 7.18 How can I access a dynamic variable while a similarly named lexical is in scope? PerlFAQ Server Perl Misc 0 03-27-2011 10:00 AM
FAQ 7.18 How can I access a dynamic variable while a similarly named lexical is in scope? PerlFAQ Server Perl Misc 0 01-21-2011 05:00 PM
User-defined exception: "global name 'TestRunError' is not defined" jmike@alum.mit.edu Python 1 07-10-2008 12:37 PM
Using parenthesis with defined (#if defined(...)) Angel Tsankov C++ 1 04-05-2006 10:00 PM
#if (defined(__STDC__) && !defined(NO_PROTOTYPE)) || defined(__cplusplus) Oodini C Programming 1 09-27-2005 07:58 PM



Advertisments