What value does this function return?

What value does this function return? Java has the same behavior.


def test():
    try:
        return 0
    finally:
        return 1



  1. in .Net you cannot have a return statement inside the finally block. it makes sense.

    -Tamer

  2. mb

    Python returns 0.

  3. mb

    Sorry, it’s 1.

  4. I guessed “1” before I tried it, and my guess held up. My internal reasoning was the old mnemonic from when I first learned Java years ago — “the ‘finally’ block runs NO MATTER WHAT”.

  5. A

    Ruby gives 1 too.






%d bloggers like this: