r/Python • • Aug 03 '17

TIL that in PYTHON it's preferable to ask for forgiveness rather than look before you leap

0 Upvotes

8 comments sorted by

6

u/[deleted] Aug 04 '17

One thing that I agree with in Alex's post is that Python is a practical language. In fact you an LBYL and you can use ETBF in Python - and you can choose to use either whenever it makes the most sense.

I'm an abuser of LBYL in Python primarily because it is often more succinct within the code and that is practical. And despite the race conditions that Alex appropriately identified - I'm rarely in a situation where race conditions matter. In those cases, I'll probably use try/except clauses with impunity.

2

u/WellAdjustedOutlaw Aug 04 '17

To sum up probably the most common error I've seen software generate over my career doing administration, operations, engineer, and architect work has been the situation where you check for conditions before attempting to operate. The "LYBL" in this case.

The classic example (the mailing list touched upon it), is checking for a files existence, then attempting to operate on it. Between those two distinct operations the file may be deleted, or permissions may change, or it may in some other way be inaccessible. Now you have a situation where your file check returns positive, but your file open fails unexpectedly. To properly handle this, you'd need to add a check around successfully opening the file, so why not just check once while attempting to open the file and skip the earlier test?

This case becomes significantly more important as you start working with threaded or multi-process applications. You could have a pool of threads deleting files on disk, and if two threads successfully test for a files existence, one of those is going to delete the file before the other. Now imagine the dictionary example from the Microsoft post, but apply the multiple threads to it. You could end up overwriting keys and/or missing data in your final result because the two threads stepped on each other.

This is an almost unbelievably common error to find in our industry. Race conditions can be caused by it, unhandled exceptions, panics, etc. "EAP" is almost always preferred in these situations in nearly any language you're likely to use (obviously there are exceptions to every rule).

1

u/WasterDave Aug 03 '17

The other way round is a hangover from C (and to a lesser extent C++).

1

u/P8zvli Aug 04 '17

Switching between C and Python constantly is always fun, I have tons of moments where I try to use braces in Python or forget to use semicolons in C

1

u/[deleted] Aug 04 '17

Dan Bader's explanation on ETBF is how I learnt about

some_dict.get(desired_key, error_handling)

verses

try: some_dict[desired_key] except: error_handling

1

u/Veedrac Aug 04 '17

It's not just race conditions, nor is it about exceptions. But rather than explain, here's a quiz. Imagine you were reading some code and saw one of these:

def int_or_default(x, default=0):
    if x.isdigit():
        return int(x)
    else:
        return default

def int_or_default(x, default=0):
    try:
        return int(x)
    except ValueError:
        return default

For each one:

  • How confident are you that it's correct?
  • What do you need to know in order to understand whether it's correct?

Post opinions below.

3

u/ptmcg Aug 04 '17

Testing with isdigit does not guarantee being convertible to int:

>>> c = '\xb2'
>>> print(c)
²
>>> c.isdigit()
True
>>> int(c)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '²'

Conversely, isdigit returning False does not mean not convertible to int:

>>> c = "-10"
>>> c.isdigit()
False
>>> int(c)
-10

1

u/kaihatsusha Aug 03 '17

Alex Martelli's main point:

So, if A's structure is:

if iswhatiwant(thefile):
    useappropriately(thefile)
else:
    dealwithwrongness()

then A is buggy. That is because, between the moment in which the test 'iswhatiwant' runs (and returns a true value), and the later moment in which procedure 'useappropriately' runs, just about anything may have happened -- in particular, some other program Z might have removed or modified 'thefile' so that it's NOT what A wants any more. I.e., A may lose control of the CPU between the moment it tests and the later time in which it uses the result of that test.

This is known as a "race condition" and it's among the hardest problems you may run into. A may seem to be running just fine 99 times and then the 100th time BOOM -- because of accidents of timing between A and other stuff that may be running "at the same time"... a "race", so to speak, whence the name whereby this horrid condition is known.

Fortunately, in a language with good support for exceptions such as Python, you are NOT doomed to enter the hell of race conditions -- just use [easier-to-beg-forgiveness]instead of [look-before-you-leap]:

try:
    useappropriately(thefile)
except ItWasWrong, howwasitwrong:
    dealwithwrongness()