[Python-Dev] Extended Function syntax
Bernhard Herzog
bh@intevation.de
03 Feb 2003 16:06:03 +0100
"Moore, Paul" <Paul.Moore@atosorigin.com> writes:
> Consider
>
> try:
> f = open("blah.txt", "r")
> ...
> finally:
> f.close()
This should actually be
f = open("blah.txt", "r")
try:
...
finally:
f.close()
Otherwise if open raises an exception the finally clause will most
likely raise an exception as well.
> and with
>
> f = autoclose("blah.txt", "r")
> with f:
> ...
>
> The problem with the third case as compared to the second is that
> using autoclose() rather than open() has *no value* in the absence
> of the with statement. And contrariwise, if x is an object which
> hasn't been designed for use with the "with" construct, "with x"
> is meaningless.
Likewise, if x is not designed to be used with "for", "for i in x" is
meaningless (and throws an exception).
> So I guess what I'm saying is that "with ... autoclose" only
> make sense in combination.
I think "return autoclose(...)" will be used in function that create
objects for use in with clauses.
> Putting the two on different lines loses
> that connection. If you need to name the result of the autoclose()
> call, you either allow the with construct to include the assignment,
> or you lose that conceptual link.
IMO whether with should allow assignment is really completely independed
of "with". It's just the general design questions of whether to have
assignment expressions at all.
Bernhard
--
Intevation GmbH http://intevation.de/
Sketch http://sketch.sourceforge.net/
MapIt! http://www.mapit.de/