in place list modification necessary? What's a better idiom?
MRAB
google at mrabarnett.plus.com
Tue Apr 7 09:36:54 EDT 2009
andrew cooke wrote:
> R. David Murray wrote:
>>> [...]
>>> try:
>>> dimensions.append(float(s))
>>> except:
>>> dimensions.append(float(quantization[s]))
>> No, no, no; never use a bare except! :)
>
> can you explain why? i can't think of any reason why the code would be
> better catching a specific exception.
>
> as a general rule, maybe, but in this particular case i can't see a reason
> - so i'm not sure if you're just pedantically following rules or if i've
> missed something i should know.
>
A bare exception will catch _any_ exception, even those you didn't
expect (technical term: "bug" :-)); for example, if you had written:
try:
dimension.append(float(s))
except:
dimensions.append(float(quantization[s]))
it would silently catch "NameError: name 'dimension' is not defined" and
perform the fallback action.
More information about the Python-list
mailing list