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

weissman.mark at gmail.com weissman.mark at gmail.com
Tue Sep 18 16:50:53 EDT 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.



More information about the Python-list mailing list