using (was Re: The Python Way)

Alex Martelli aleax at aleax.it
Tue Apr 2 10:54:58 EST 2002


Aahz wrote:
        ...
>     using MyLock():
>         <code>
> 
> The using construct would automatically call the start() and finish()
> methods of the MyLock() instance, no matter what exceptions got raised
> in <code>.

I definitely wouldn't mind a slightly more compact equivalent of

try:
    <code>
finally:
    <some guaranteed termination>

particularly with the ability of displaying the termination code
that's guaranteed to run BEFORE the code that's being tried (when
it's important to let the reader know at once what code he or she
can take as guaranteed-to-run-at-end).  But is the new keyword
'using' and the specific convention of .start() and .finish()
calls all warranted?  Wouldn't the ability to write the finally
clause before the try one suffice for the "display the key part
first" stylistic purpose, for example?  And you might then (just
maybe) omit the try: line, i.e. have a new 'finally:' statement
where:

finally:
    <guaranteed termination stuff>

<code>

has the same semantics as:

try:
    <code>
finally:
    <guaranteed termination stuff>


I dunno -- maybe this isn't clear enough, or error-prone wrt
indentation errors of some try clause and/or its body.  But I
suspect that if we need a new keyword for this, we ain't gonna
get it (just pessimistic, maybe...:-)


Alex




More information about the Python-list mailing list