Re Interest check in some delicious syntactic sugar for "except:pass"
Mel
mwilson at the-wire.com
Wed Mar 3 10:43:25 EST 2010
Tim Chase wrote:
> I admit there are times I've done something similar, usually with
> what I call my "int0" and "float0" utility functions which
> roughly translate to "give me a stinkin' int/float and if
> something goes wrong, give me 0, but the return result better
> darn well be an int/float!" functions. But as you describe and
> was later commended as a right-ish way to approach it,
> abstracting that off into a function with minimal code in the
> try: block is the right way to go.
[ ... ]
> def int0(v):
> """converts v to a int, or returns 0 if it can't"""
> try:
> return int(v)
> except: # usually a ValueError but catch all cases
> try:
> # int("1.1") fails so try float()ing it first
> return int(round(float(v)))
> except:
> return 0
>
> def float0(v):
> """converts v to a float, or returns 0 if it can't"""
> try:
> return float(v)
> except:
> return 0.0
I think replacing `except:` with `except StandardError:` would work as well,
and be correct, too.
Mel.
More information about the Python-list
mailing list