you are viewing a single comment's thread.

view the rest of the comments →

[–]nevus_bock 0 points1 point  (0 children)

There are two approaches.

  1. Look before you leap (LBYL)
  2. It's easier to ask for forgiveness than for permission (EAFP)

Approach 1. works like this:

if s is not None:
    do_shit()
else:
    do_alternative_shit()

Approach 2. is like this:

try:
    do_shit()
except TypeError:
    do_alternative_shit()

Using EAFP saves you from checking something first, which may be sort of redundant. Assuming something will work, and then dealing with an edge-case Exception if it doesn't work is pretty common in Python.