Raising objects

Steven Taschuk staschuk at telusplanet.net
Wed Apr 30 00:45:46 EDT 2003


Quoth Alex Martelli:
  [...]
> No, in 2.2 (and 2.3 beta 1) exceptions must still be classic-classes
> or instances thereof [or strings for backwards compatibility with the
> dark and distant past], and should be derived from Exception (but this
> is a recommendation, not enforced).  I do not know when (or if) this
> restriction will be removed -- in particular, because I do not know
> of "use cases" where the ability to raise new-style classes or instances
> thereof would be a major boon to an application.  Maybe you have some...?

I don't have a real use case either, but it does seem rather
arbitrary.  Is there an implementation reason for this restriction?

A somewhat silly possible use:  A depth-first search to find a
single object satisfying a given property.  An implementation
(ab)using exceptions for control flow:

    def _search(node):
        if condition(node):
            raise node
        else:
            for child in childrenof(node):
                _search(child)

    def findone():
        try:
            _search(root)
        except Node, found:
            return found
        else:
            return None

If the objects in the search tree are instances of new-style
classes, the restriction would be inconvenient.  It would be
simple enough -- though ugly -- to wrap them in an old-style
class for raising, of course.

-- 
Steven Taschuk                               staschuk at telusplanet.net
"[T]rue greatness is when your name is like ampere, watt, and fourier
 -- when it's spelled with a lower case letter."      -- R.W. Hamming





More information about the Python-list mailing list