Raising objects

Michael Chermside mcherm at mcherm.com
Fri May 2 09:51:44 EDT 2003


Michael Chermside writes:
> Not that performance is the the be-all and end-all of design, nor
> is exception handling the most important place to optimize, but
> it's kinda neat that it saves this unnecessary object creation
> JUST SO LONG AS it goes ahead and creates the object if you ever
> do anything which would access that object.

Steven Taschuk replies:
> That's an interesting idea.  But I'm not sure the performance
> advantage (if there is one) is worth the weirdness.
> 
> For example, [excellent examples of problems]

Hmmm. I was not SUGGESTING this behavior, I thought that it
already EXISTED. Let me try it out:

>>> class TestException(Exception):
...     def __init__(self):
...         print "initializing"
...
>>> try:
...     raise TestException()
... except TestException:
...     print "caught it"
...
initializing
caught it
>>> try:
...     raise TestException
... except TestException:
...     print "caught it"
...
initializing
caught it

Okay... seems like it creates the object. In that case, I
strongly object to this idea... better to just create the
object every time (it's an optimization that we don't need!).
Still, I wonder where I'd gotten the idea that the exception
wasn't always instantiated. Perhaps that can happen with
exceptions that are raised but then caught all within C
code?

-- Michael Chermside






More information about the Python-list mailing list