When writing concurrent/multithreaded code in Python, is it especially important to follow the "Easier to Ask for Forgiveness than Permission" (EAFP) idiom, rather than "Look Before You Leap" (LBYL)? Python's exceptionally dynamic nature means that almost anything (e.g., attribute removal) can happen between looking and leaping---if so, what's the point? For example, consider
# LBYL
if hasattr(foo, 'bar'):
baz = foo.bar
versus
# EAFP
try:
baz = foo.bar
except AttributeError:
pass
In the LBYL example, the bar
attribute could disappear from foo
before the actual call to foo.bar
is made, so do you gain anything from the check? If there's a risk the attribute might disappear, you need locks and/or try/except clauses anyway.
One possible argument here is that this example makes the extremely pessimistic assumption that "antagonistic code" is running that could yank the rug from under you at any moment. In most use cases, this is highly unlikely.