Artificial truth

The more you see, the less you believe.

[archives] [latest] | [homepage] | [atom/rss]

Fun with python
Thu 15 November 2012 — download

What does this snippet print ?

def my_function():
    try:
        return 1/0
    except Exception:
        return "Exception"
    finally:
        return "Finally"

print my_function()

Result:

$ python snippet.py
finally

This black magic is explainedin the documentation:

When return passes control out of a try statement with a finally clause, that finally clause is executed before really leaving the function.

You can know ask this uncomfortable questions to your favorite python developer :)