[Python-Dev] Avoid formatting an error message on attribute error

Eric Snow ericsnowcurrently at gmail.com
Thu Nov 7 20:20:36 CET 2013

On Thu, Nov 7, 2013 at 11:44 AM, Brett Cannon <brett at python.org> wrote:
> Lazy message creation through
> __str__ does leave the message out of `args`, though.

If that's an issue you could make args a (settable) property that
dynmically returns str(self) if appropriate:

    def args(self):
        actual = super().args
        if actual or self.name is None:
            return actual
        return (str(self),)

> In a perfect world (Python 4 maybe?) BaseException would take a single
> argument which would be an optional message, `args` wouldn't exist, and
> people called `str(exc)` to get the message for the exception. That would
> allow subclasses to expand the API with keyword-only arguments to carry
> extra info and have reasonable default messages that were built on-demand
> when __str__ was called. It would also keep `args` from just being a dumping
> ground of stuff that has no structure except by calling convention (which is
> not how to do an API; explicit > implicit and all). IOW the original dream
> of PEP 352 (http://python.org/dev/peps/pep-0352/#retracted-ideas).

This reminds me that I need to revisit that idea of reimplementing all
the builtin exceptions in pure Python. :)


More information about the Python-Dev mailing list