Nerd Sniping: What does the code do at the end of the page?
neil.fraser.name
neil.fraser.name
It's not really a "paradox" so much as it is very bad style to "return" from a "finally".
Edit
My partner Dave has a much better trick question:
def foo
begin
return false
ensure
next
end
end
"You win", I told him. "Nah, Google won. But nobody really wins if you write code like that."I realize that's not the point, but I figured I'd mention it anyway.
try { return true; } finally { return false; }
Which returns false. I guess this is because of the guarantee that the finally block will be run, no matter what. The JS interpreter must be catching the return and holding it pending the execution of the finally block.
Checking with
try { return true; } finally { alert('hi'); }
Shows this to be true. (It both returns true AND alerts 'hi'.)
When you call non-trivial 3rd party code from your finally block, how do you ensure it doesn't throw? Or what do you do if it does? Indefinitely nested try blocks?
The ultimate screen would be to follow the candidate home and see if they Google or test the answer.
try: return True finally: return False
-- There's an error in your program: * 'return' outside function ...
Just sayin'
>>> def test():
... try: return True
... finally: return False
...
>>> test()
False