[Python-Dev] exec/with thunk-handling proposal

jo@jan.csie.ntu.edu.tw jo@jan.csie.ntu.edu.tw
Tue, 4 Feb 2003 00:12:16 +0800


> From: "Gerald S. Williams" <gsw@agere.com>
> holger krekel wrote:
> > I think we can may get away with only a "weak" keyword
> > and allow the aforementioned encapsulation of execution 
> > events into an object like this:
> > 
> >     exec expr [with params]: suite
> 
> I was about to propose something similar. This looks like
> it can be unified with what Guido proposed.
> 
> > regards and interested in comments,
> 
> An observation:
> 
> Used like this, "with" looks like an alias for "lambda"
> (providing default values for all parameters).
> 

Yes. If we have multiline lambda, we could write:

def autoclose(f, action):
    try:
        action(f)
    finally:
        f.close()

autoclose(open("blah.txt"),
    lambda f:
        for line in f:
            print line
)