How Best to Coerce Python Objects to Integers?

Chris Angelico rosuav at gmail.com
Tue Jan 3 17:38:50 EST 2017


On Wed, Jan 4, 2017 at 8:41 AM,  <breamoreboy at gmail.com> wrote:
> Hi all, I'd suggest that this http://blog.pyspoken.com/2017/01/02/how-best-to-coerce-python-objects-to-integers/ is not one of the greatest articles ever written about Python exception handling.  Other opinions are welcome.
>

"""
So there you have it. I’m happy with this function. It feels
bulletproof. It contains a naked except, but that only covers one
simple line of code that’s unlikely to hide anything nasty.
"""

Yep! It's perfect. He has successfully made a function that won't leak
any exceptions. Congratulations!

The novice believes that the first priority is to stop the program
from crashing. The expert understands that crashing (especially with
an exception, but even a segfault) is actually very helpful and
useful.

I love the logic that a bare 'except' is okay as long as it's only
covering one single line of code. There's almost enough truth in that
to be meaningful, while still completely missing the point that a
NameError is almost certainly a bug no matter where it crops up.

ChrisA


More information about the Python-list mailing list