0

Using Python 3.4, wondering how does this loop processing?

while SyntaxError:
    print ("Hi")

It's an infinite loop, how does this while loop running? It's an exception but..?

1
  • 3
    Because the SyntaxError class evaluates truthy in a Boolean context. This is equivalent to while True, that it's an Exception is irrelevant. Commented Jan 29, 2015 at 8:49

2 Answers 2

4

Exceptions are just objects unless they're raised - bool(SyntaxError) is True, so your loop is effectively while True:

Sign up to request clarification or add additional context in comments.

Comments

3

Boolean value of SyntaxError is True.

>>> bool(SyntaxError)
True

That is why while loop is going in infinite loop because while SyntaxError: is equivalent to while True: condition.

while SyntaxError:
    print ("Hi")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.