« first day (2138 days earlier)      last day (1075 days later) » 

12:46 AM
I think 3 is enough.
@flawr what is it? Logic says it should be 2, but I guess that’s wrong, otherwise you wouldn’t ask...
> If the try statement reaches a break, continue or return statement, the finally clause will execute just prior to the break, continue or return statement’s execution.
If a finally clause includes a return statement, the returned value will be the one from the finally clause’s return statement, not the value from the try clause’s return statement.
So it returns 7.
 
 
11 hours later…
12:10 PM
@CrisLuengo 10 points for Hufflepuff:)
@CrisLuengo It feels a little bit paradox, as in most situations it is valid to think about it that way, that return always immediately finishes the excution of the function.
 
 
4 hours later…
4:01 PM
@flawr Yes, return should immediately finish the function. It looks like they created a paradox with their finally keyword. At least they documented the paradox and made the situation unambiguous that way. Still, IMO a finally should be prohibited from having a return.
 
4:26 PM
Finally is there to ensure cleanup is possible, no matter what
If anything, the return should be forbidden in try :P
 
4:40 PM
That also means the recursion limit should be removed:P
 
5:40 PM
@AndrasDeak I understand that. And this is why finally should run just before any return. But you don’t need a return within a finally.
 
6:33 PM
@CrisLuengo but you can't clean up if you allow code to be executed after finally (i.e. wherever the other return is)
and if the exception was raised in whatever was supposed to be returned then you also can't just return that after finally
of course you'd also have to forbid returns in except and else if you allow it in finally
But all of this is academical because no sane code should have conflicting returns.
 
I think it’s pretty straightforward: you compute the return value, clean up, then return the value. Maybe my C++ background makes this obvious to me?
If an exception is thrown, then the return in try is never executed, and you execute the one in catch if there’s one. I see no problem with return statements in try, catch or else. I see a problem with one in finally, because producing a return value is not part of cleaning up.
In C++ the cleaning up is implicit. Local variables are destructed at the end of the function, when they go out of scope. This is the cleanup. Object destructors are run, closing files and so on. This happens after all code in the function is run, including whatever is computed in the return statement itself.
 
@CrisLuengo that's actually the same in python and you almost never see finally clauses
I imagine it has significance in context involving databases and the like
and of course garbage collection makes it a bit more uncertain when objects get destroyed, but it almost always happens instantly
@CrisLuengo but what happens when you have try: return 1/0, raising an exception? You can't just go back after finally and return 1/0 anyway.
 
 
1 hour later…
8:04 PM
@AndrasDeak In C++ that would evaluate 1/0, then throw an exception and not evaluate the return itself. A line of code is not a single operation, it can be many operations in a predetermined sequence.
@AndrasDeak I understand that Python does reference counting, so can destroy objects immediately when the reference count reaches 0. But then needs garbage collection for the cases where reference loops make it so that counts cannot reach zero even if the program cannot reference the objects in the loop anymore.
 
@CrisLuengo yeah, it's the same in python. But then you'd have to return something in finally :P
@CrisLuengo yup
 

« first day (2138 days earlier)      last day (1075 days later) »