More on "with" statement

Alex Martelli aleaxit at
Sat Jul 14 16:30:39 CEST 2001

"Levente Sandor" <nospam at> wrote in message
news:%qV37.19159$Kf3.249506 at
> >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

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
>>> x.pok

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


More information about the Python-list mailing list