On 1/21/2020 2:25 PM, Chris Angelico wrote:

Hmm. Maybe this should be a recipe in the docs, or something: "how to
make a repr that reconstructs an object".

def describe(obj, attrs):
     attrs = [f"{a}={getattr(obj, a)!r}" for a in attrs]
     return f"{type(obj).__name__}({", ".join(attrs)})"

def __repr__(self):
     return describe(self, "name age spam ham location".split())

This comes up often enough that I think it'd be a useful thing to
point people to.

reprlib would seem to be the place for this, if it's not already there.

Eric
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/FCEIEYV3I7AC42BE5NH3I6NP7X4GBDS5/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to