try...finally is more powerful than I thought.

Lee Harr missive at frontiernet.net
Sat Nov 8 02:33:35 CET 2003


On 2003-11-07, Peter Hansen <peter at engcorp.com> wrote:
> Lee Harr wrote:
>> 
>> >>> def res():
>> ...     print 1
>> ...     try:
>> ...         print 2
>> ...         return 3
>> ...         print 4
>> ...     finally:
>> ...         print 5
>> ...
>> >>> res()
>> 1
>> 2
>> 5
>> 3
>> 
>> interesting.
>
> Why?  It just proves that "finally" works, and
> executes when the return statement is encountered.
>
> (The point of my examples was to show that finally can
> actually override the value that would otherwise have been
> returned.  I thought that was interesting, but unfortunately
> I'm not sure what your example adds to the previous two posts.
> Maybe I'm just missing the point...)
>


Yes. I just found the entire thread interesting. I know that
people who are unfamiliar with the try ... finally syntax can
easily fire up the interpreter and play around with it for
themselves, but I thought the "basic functionality" example
might make the rest of the thread more clear.

I have written a lot of python code and have never used finally.





More information about the Python-list mailing list