Generating Python Exceptions Classes

  • Written by: Marko Samastur
  • Published on:
  • Category: General development, Python

It’s been a while since I used Python for anything larger than scripts few tens lines long, so it feels really great to do it again. I did discover however that I became a bit rusty. Not so much in not being able to achieve what I want as not being sure that I do it in a sensible and pythonic way.

I’ve been working on a private project where I came to a following problem. API calls can trigger various responses, somewhat like HTTP , containing status codes together with a short description. Every faulty response should trigger its own exception, which led me to my first implementation:


class Unauthorized(Exception):
    status = 101
    value = "Unauthorized."

I didn’t like it even though it looks and behaves like it should. What I wanted was a better overlook of possible responses in a way where I have to make any possible changes easily and only at one place.

My second attempt was auto-generating exception classes using type. Since class definition took only a line instead of three, it certainly achieved better transparency, but I still had to make changes at two places.

Final step was to auto-generate classes in a loop. To do this I attached them to module namespace using globals() dictionary. Actually I used __builtin__ one at first, but it obviously didn’t work that great.

So this is what I have now. It works and achieves my goals. I only need to change dictionary to add a new response or change existing one and it could hardly be more readable.

But is it pythonic enough? If not, what would be, apart from traditional way described in first step?