Python Quiz II

Oct 06, 2007 08:42

Guess what this program does:

def throw():
raise Exception()

print 2 in [] == throw()

Leave a comment

Comments 7

rizen October 6 2007, 17:34:29 UTC
false?

I'm on my phone, otherwise I'd check it.

Reply

thespeedbump October 7 2007, 05:19:42 UTC
Correct.

Why?

Reply

rizen October 7 2007, 09:46:27 UTC
I forgot the reason that I had at the time, but I looked again and I think what must be happening is that it's returning false because 2 is not in [], and then it short-circuits there and never evaluates the function. I can't think of any other reason why the exception is not being raised.

Reply

rizen October 7 2007, 10:29:31 UTC
Now that I'm home and able to test out that theory, I think it's likely the reason, as "2 in []" does indeed return False. (I couldn't remember if that was even a valid boolean test in Python.)

Reply


rizen October 6 2007, 21:14:53 UTC
Hot shit, I was right for once.

Reply


Leave a comment

Up