The Most Diabolical Python Antipattern

Chris Angelico rosuav at gmail.com
Thu Jan 29 18:25:33 EST 2015


On Fri, Jan 30, 2015 at 4:32 AM, Tim Chase
<python.list at tim.thechases.com> wrote:
> I have one that I call int0()
> that is my "give me a freakin' int" function which is something like
>
>   def int0(val):
>     try:
>       return int(val)
>     except:
>       return 0
>
> because I deal with a lot of CSV data from client/vendor that has
> blanks, "NULL", "---", and plenty of other rubbish to suggest
> something that, for my purposes is really just a 0.

What's wrong with "except (TypeError, ValueError):" here? Or even just
"except ValueError:", which seems to be what you want? The bare except
is still a problem.

ChrisA



More information about the Python-list mailing list