Convert '165.0' to int

Steven D'Aprano steve+comp.lang.python at
Sun Jul 24 09:21:28 CEST 2011

Billy Mays wrote:

> I'll probably get flak for this, but damn the torpedoes:
> def my_int(num):
>      import re
>      try:
>          m = re.match('^(-?[0-9]+)(.0)?$', num)
>          return int(

As a toy for learning about regexes, that's fine, but I trust you would
never use that in production. There are less verbose ways of wasting time
and memory.

>      except AttributeError:
>          #raise your own error, or re raise
>          raise

"except Foo: raise" is pointless; a bare raise is only useful if you need to
do something before, or instead of, re-raising the current exception.

except HeatingError:
    if isempty(kettle):

Catching an exception only to unconditionally re-raise it is only good for
generating more heat in the CPU and making other developers laugh at you :)


More information about the Python-list mailing list