raising classes

Mark McEahern marklists at mceahern.com
Mon Aug 19 19:02:43 EDT 2002


[Paul Rubin]
> Are you saying that
>
>    class foo(Exception): pass
>    ...
>    raise foo
>
> raises foo's class object rather than instantiating foo?
>
> What happens on
>
>   class foo(Exception): pass
>   class bar(foo): pass
>   try:
>      ...
>        raise bar
>   except foo:
>     print 'foo exception'

Extending your question/idea a bit, I offer this, which hopefully will serve
to answer the question:

#! /usr/bin/env python

class foo(Exception):
    def __init__(self, *args, **kwargs):
        print "%s(%s, %s)" % (self.__class__.__name__, args, kwargs)

class bar(foo):
    def __init__(self, *args, **kwargs):
        foo.__init__(self, *args, **kwargs)

try:
    raise bar
except foo:
    print 'foo exception'

Cheers,

// mark

-





More information about the Python-list mailing list