In PHP there a function called isset() to check if something (like an array index) exists and has a value. How about Python?
I need to use this on arrays because I get “IndexError: list index out of range” sometimes.
I guess I could use try/catching, but that’s a last resort.
Look before you leap (LBYL):
Easier to ask forgiveness than permission (EAFP):
In Python, EAFP seems to be the popular and preferred style. It is generally more reliable, and avoids an entire class of bugs (time of check vs. time of use). All other things being equal, the
try/exceptversion is recommended – don’t see it as a "last resort".This excerpt is from the official docs linked above, endorsing using try/except for flow control: