in place list modification necessary? What's a better idiom?

andrew cooke andrew at acooke.org
Tue Apr 7 10:37:06 EDT 2009


MRAB wrote:
> 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.

true, i hadn't considered coding errors (i was thinking that something
global, like out of memory, would fail again anyway).  andrew




More information about the Python-list mailing list