More on "with" statement

Alex Martelli aleaxit at yahoo.com
Sat Jul 14 10:30:39 EDT 2001


"Levente Sandor" <nospam at newsranger.com> wrote in message
news:%qV37.19159$Kf3.249506 at www.newsranger.com...
    ...
> >def with(obj, statements, globs=None):
> >    if globs is None: globs=globals()
> >    exec statements in vars(obj), globs
>
> Sorry but I can't see how it simulates the 'with' statement in Pascal or
VB.

That's probably because I had switched the two dictionaries
in the exec statement -- here's a corrected, simpler version:

>>> def with(obj,stmt):
...   exec stmt in globals(), vars(obj)
...
>>> with(x, "pak=23; pok=24")
>>> x.pak
23
>>> x.pok
24
>>>

In lieu of globals() one surely wants to default to the *caller*'s
globals, sys._getframe(1).f_globals, of course.


Alex






More information about the Python-list mailing list