raising classes

Paul Rubin phr-n2002b at NOSPAMnightsong.com
Mon Aug 19 18:18:51 EDT 2002


Tim Peters <tim.one at comcast.net> writes:
> You can if you want to (try it!).  I expect some people don't just because
> it's more typing, while others don't because instantiating an instance of a
> class is more expensive than merely using the (already full constructed)
> class object itself.

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'

Raising bar is supposed to be caught by "except foo".  But bar and foo
are both class objects, so bar is not an instance of foo.

Are you saying the 'except' statement treats class objects specially,
and figures out if a thrown class object is a subclass of the caught
class, while using something like isinstance if you throw an instance
rather than a class object?

I guess that's not so terrible; there's just something "more than one way
to do it"-ish about it.



More information about the Python-list mailing list