Why does `if Exception` work in Python

Jasper

In this answer https://stackoverflow.com/a/27680814/3456281, the following construct is presented

a=[1,2]
while True:
    if IndexError:
        print ("Stopped.")
        break
    print(a[2])

which actually prints "Stopped." and breaks (tested with Python 3.4.1).

Why?! Why is if IndexError even legal? Why does a[2] not raise an IndexError with no try ... except around?

Daniel Roseman

All objects have a boolean value. If not otherwise defined, that boolean value is True.

So this code is simply the equivalent of doing if True; so execution reaches the break statement immediately and the print is never reached.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does this not work? (Python)

From Dev

Why does this indentation work? python

From Dev

Why >= evaluation does not work Python

From Dev

python iterator: why does this work?

From Dev

Why does this python code work?

From Dev

Why does this Hashmap Iteration not work? I get a NullPointer Exception

From Dev

How does this exception work?

From Dev

Why does this bash call from python not work?

From Dev

Why python re module does not work with @?

From Dev

why the following regular expression does not work in Python?

From Dev

Why does my python regex not work?

From Dev

why does this work in c but not in python: (a = 5) == 5

From Dev

Why does `a<b<c` work in Python?

From Dev

Why does the following Python generator expression work?

From Dev

why does if not in(x,y) not work at all in python

From Dev

Why Python append function does not work in this case?

From Dev

Why does this SocketServer work in Python but not IronPython?

From Dev

Why does this bash call from python not work?

From Dev

Python Why Does This While Loop Control Work

From Dev

Why python re module does not work with @?

From Dev

Understanding python decorator. why does this not work?

From Dev

Why does my Python script not work at all?

From Dev

Why autocomplete sometimes does not work in python IDE

From Dev

Why does my IF statement not work on python?

From Dev

Reversing a python string - why does this work?

From Dev

Python requests - Exception Type: ConnectionError - try: except does not work

From Dev

Why does this Python code work as it does? Please explain

From Dev

Why does this code work perfectly well in Python 3.3 but not in Python 2.7?

From Dev

Why does this code work perfectly well in Python 3.3 but not in Python 2.7?

Related Related

  1. 1

    Why does this not work? (Python)

  2. 2

    Why does this indentation work? python

  3. 3

    Why >= evaluation does not work Python

  4. 4

    python iterator: why does this work?

  5. 5

    Why does this python code work?

  6. 6

    Why does this Hashmap Iteration not work? I get a NullPointer Exception

  7. 7

    How does this exception work?

  8. 8

    Why does this bash call from python not work?

  9. 9

    Why python re module does not work with @?

  10. 10

    why the following regular expression does not work in Python?

  11. 11

    Why does my python regex not work?

  12. 12

    why does this work in c but not in python: (a = 5) == 5

  13. 13

    Why does `a<b<c` work in Python?

  14. 14

    Why does the following Python generator expression work?

  15. 15

    why does if not in(x,y) not work at all in python

  16. 16

    Why Python append function does not work in this case?

  17. 17

    Why does this SocketServer work in Python but not IronPython?

  18. 18

    Why does this bash call from python not work?

  19. 19

    Python Why Does This While Loop Control Work

  20. 20

    Why python re module does not work with @?

  21. 21

    Understanding python decorator. why does this not work?

  22. 22

    Why does my Python script not work at all?

  23. 23

    Why autocomplete sometimes does not work in python IDE

  24. 24

    Why does my IF statement not work on python?

  25. 25

    Reversing a python string - why does this work?

  26. 26

    Python requests - Exception Type: ConnectionError - try: except does not work

  27. 27

    Why does this Python code work as it does? Please explain

  28. 28

    Why does this code work perfectly well in Python 3.3 but not in Python 2.7?

  29. 29

    Why does this code work perfectly well in Python 3.3 but not in Python 2.7?

HotTag

Archive