[Python-ideas] automation of __repr__/__str__ for all the common simple cases

Nick Coghlan ncoghlan at gmail.com
Thu Feb 16 01:03:38 CET 2012

On Thu, Feb 16, 2012 at 1:34 AM, Nathan Rice
<nathan.alexander.rice at gmail.com> wrote:
> I think that a generic __repr__ has been reinvented more times than I
> can count.  I don't think a generic __str__ is a good thing, as it is
> supposed to be a pretty, semantically meaningful.  I don't really see
> anywhere in the standard library that such a feature would make sense
> though.

Python 3's reprlib already provides some tools for writing
well-behaved __repr__ implementations (specifically, the
reprlib.recursive_repr decorator that handles cycles in container

I actually have a recipe for simple "cls(arg1, arg2, arg3)" style
__repr__ output on Stack Overflow:

> There is a similar recipes in SQL Alchemy, and I've seen them in a few
> other popular libs that I can't remember off the top of my head.

The unfortunate part of dict-based __repr__ implementations is that
the order of the parameter display is technically arbitrary.


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-ideas mailing list