Python Mystery Theatre -- Episode 1: Exceptions
Peter Hansen
peter at engcorp.com
Sat Jul 12 05:43:36 EDT 2003
Erik Max Francis wrote:
>
> > ACT II --------------------------------------------
> > >>> class MyMistake(Exception):
> > ... pass
> >
> > >>> try:
> > ... raise MyMistake, 'try, try again'
> > ... except MyMistake, msg:
> > ... print type(msg)
> > ...
> >
> > <type 'instance'>
>
> I'm not sure what mystery you're trying to get at here. This is what
> Python prints for all instances:
[snip]
He's showing a case where a programmer thought he/she was using
a sort of "parallel form" with the "MyMistake, 'try, try again'"
part and the "MyMistake, msg" part.
The programmer expected print type(msg) to show "<type 'str'>".
This is probably an example of an error promoted by leaving the
redundant "raise Class,args" form of exception-raising in Python,
instead of having a single obvious way: "raise Class(args)" as
would be more Pythonic. ;-)
-Peter
More information about the Python-list
mailing list