Convert '165.0' to int
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Sun Jul 24 03:21:28 EDT 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(m.group(1))
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.
try:
boil(kettle)
except HeatingError:
if isempty(kettle):
kettle.fill(water)
boil(kettle)
else:
raise
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 :)
--
Steven
More information about the Python-list
mailing list