[Python-Dev] exec/with thunk-handling proposal
Alex Martelli
aleax@aleax.it
Tue, 4 Feb 2003 13:03:24 +0100
On Tuesday 04 February 2003 12:54 pm, holger krekel wrote:
...
> f1=open(inputfn)
> with autoclose(f1):
> f2 = open(outputfn, 'w')
> with autoclose(f2):
> for line in f1:
> ...
> f2.write(line)
>
> I think there should be a better solution for multiple ressources.
I agree this is slight too cumbersome, and deeply wish it was
feasible to go with:
with f1=autoclose(inputfn):
with f2=autoclose(outputfn, 'w'):
for line in f1:
...
f2.write(line)
i.e., nested with's would be just fine, IF binding the with'd expression
to a name was allowed as part of the with statement, by whatever sugar.
I'd rather have it mandatory and use "with junk = autolock(xx):", rather
than not having the binding in the "with" -- just as I have only modest
problems HAVING to bind a name in "for i in range(5):" even when I
don't care about i at all in the loop's body.
But if such binding is unfeasible, then maybe it's not worth having the
"with" after all -- or else we need something richer and more complicated
(and perhaps ugly) as in your proposal.
Alex