python-dev Summary for 2004-02-01 through 2004-02-29

Rocco Moretti roccomoretti at hotpop.com
Wed Mar 17 14:33:29 EST 2004


Brett C. wrote:

> ---------------------------------------------------
> Do we really need mutability for exceptions groups?
> ---------------------------------------------------
> The question was raised as to why ``except (TypeError, ValueError):``
> is acceptable while ``except [TypeError, ValueError]:`` (in other
> words why use tuples to group exceptions for an 'except' statement and
> not allow lists).  The question was in relation as to whether it had
> something to do with a tuple's immutability.
> 
> Guido said it all had to do with a visual way of grouping tuples and
> nothing to do with what the underlying container was.  If he had it to
> do over again he would rather change it so that ``except TypeError,
> ValueError:`` did the same thing as above.  That change would
> alleviate the common error of expecting the above behavior using that
> syntax but instead getting the exception instance bound to ValueError.
>  But the change is not planned for any time in the future since Guido
> has no proposal on how to change the syntax to handle the assignment
> to a local variable for the exception instance.

Generalizing it to any iterable would be nice, especially with 
situations where you have the exception types stored in a variable:

catch = (TypeError, ValueError, MyValueError)
...
try:
     #Body
except catch:
     #Error code

If you need to dynamically add exceptions, it may be easier to use a 
list, and using general iterables opens up all sorts of fun with 
generators. (Is time savings all that big of deal here?)

As far as Python3000 syntax, has anyone suggested 'as' abuse?

try:
     #Body
except TypeError, ValueError as e:
     #Error code

I'm not sure if this is compatable with the parser or not, but it looks 
nice ...

-Rocco



More information about the Python-list mailing list