I\'m not asking for personal \"religious\" opinions about this philosophy, rather something a bit more technical.
I understand this phrase is one of several litmus t
My personal non-religious opinion is that said mantra applies mainly to documented and well understood exit conditions and edge cases (e.g. I/O errors), and should never be used as a get-out-of-jail card for sloppy programming.
That said, try/except
is often used when "better" alternatives exist. For example:
# Do this
value = my_dict.get("key", None)
# instead of this
try:
value = my_dict["key"]
except KeyError:
value = None
As for your example, do use if hasattr(foo, "bar")
if your have no control over foo
and need to check conformance with your expectations, otherwise simply use foo.bar
and let the resulting error be your guide to identifying and fixing sloppy code.