New style classes as exceptions (Was: exception handing)

Jonathan Hogg jonathan at onegoodidea.com
Mon Jul 1 16:47:14 EDT 2002


On 1/7/2002 18:02, in article
7V%T8.69851$F16.33443 at atlpnn01.usenetserver.com, "Steve Holden"
<sholden at holdenweb.com> wrote:

> I fail to see how the example above claims to "subclass from Exception".

Seems pretty clear to me:

>>> class FooError( StandardError, object ):
...     pass
... 
>>> issubclass( FooError, Exception )
1
>>> f = FooError( "No foo." )
>>> f
<__main__.FooError object at 0x40a310>
>>> print f
No foo.
>>> isinstance( f, Exception )
1
>>> 

Looks like an Exception, walks like an Exception, but unfortunately can't be
raised.

> It
> certainly doesn't seem to be a very good idea to use object as one of the
> bases, since this is *known* to cause problems.

Yes, the main problem being that it just doesn't work ;-)

> Instead see, for example:
>
> >>> class MyError(IOError):
> ...     pass
> ...

I think it's obvious that the answer is Well Don't Do That. But ...

> I think it's currently a feature, but it will doubtless become a bug if this
> situation continues for too long...

... as you say, if new-style classes are the future, and certainly the
present now, then it doesn't make much sense to not be able to raise them.

Jonathan




More information about the Python-list mailing list