metaclasses and Exceptions

Mark Nottingham mnotting at gmail.com
Sat Jul 31 00:04:03 EDT 2004


aahz at pythoncraft.com (Aahz) wrote in message news:<cedv8k$ecr$1 at panix3.panix.com>...
> In article <fc92b425.0407282343.750721e at posting.google.com>,
> Mark Nottingham <mnotting at gmail.com> wrote:
> >
> >From previous discussion [1] it seems that it's not possible to raise
> >new-style objects. Does this effectively mean that it's impossible to
> >use anything with a metaclass as an exception? i.e., am I missing some
> >nifty trick that would allow me to do this in the meantime?
> 
> Why do you want to do this?

I have to define a fairly large number (~40) of exceptions, and need
to keep a dictionary keyed on an error code for each, so that the
appropriate exception can be looked up. While I can manage it
manually, it would be much cleaner and more reliable to use a
metaclass to automatically create the dictionary from class
attributes.

E.g.,

class MyExceptionNumberOne:
    code = "1"

class MyExceptionNumberTwo:
    code = "2"

lookup = {
    1: MyExceptionNumberOne,
    2: MyExceptionNumberTwo,
}

Cheers,



More information about the Python-list mailing list