More on "with" statement

Alex Martelli aleaxit at yahoo.com
Sat Jul 14 02:57:06 EDT 2001


"Levente Sandor" <nospam at newsranger.com> wrote in message
news:poP37.19002$Kf3.249024 at www.newsranger.com...
> If you absolutely need a 'with' statement...
>
> import re
> def with(object, statements):
> """ 'with' for the very lazy programmer :) :) :)
> Someone might prefer Ctrl+C, Ctrl+V, ... instead."""
> exec(re.sub('\B\.', '%s.' % object, statements))

If you're willing to stoop to an exec, then surely it's
better to use the fact that exec lets you specify local
and global dictionaries:

def with(obj, statements, globs=None):
    if globs is None: globs=globals()
    exec statements in vars(obj), globs


Alex






More information about the Python-list mailing list