<div dir="ltr"><div><div>Hi,<br><br></div>>>> from collections import namedtuple<br>>>> A  = namedtuple("A", ["foo"])<br>>>> print(A(foo=1))<br>A(foo=1)<br>>>> str(A(foo=1))<br>'A(foo=1)'<br>>>> repr(A(foo=1))<br></div><div>'A(foo=1)'<br><br></div><div>The relevant code is <a href="https://hg.python.org/cpython/file/2.7/Lib/collections.py#l356">https://hg.python.org/cpython/file/2.7/Lib/collections.py#l356</a><br><br></div><div>I propose we bring the behavior to regular classes.<br><br></div><div>Instead of<br><br>>>> class A(object):<br>...     def __init__(self):<br>...             self.foo = 1<br>...<br>>>> repr(A())<br>'<__main__.A object at 0x1090c0990>'<br><br><br></div><div>We should be able to see the current values to the display.<br></div><div>>>> repr(A())<br></div><div>'A(foo=1)'<br></div><div><br></div><div>Reasons:<br><br></div><div>1. Helps debugging (via pdb, print and logging). We no longer have to do A().foo to find out.<br></div><div>2. I don't know how often people actually rely on repr(A()) or str(A()) and parse the string so breaking compatibility is, probably low.<br></div><div>3. People who wish to define their own repr and str is welcome. Django model for example has a more explicit representation by default (although many Django users do redefine the representation on their own). datetime.datetime by default, as a library, is also explicit. So customization will come.<br><br></div><div>The main challenge:<br><br>Where and how do we
 actually look for what attributes are relevant? namedtuple can do it because it has
 __slot__ and we know in advance how many attributes are set. In regular
 class, we deal with dynamic attribute setting, single and inheritances.
 I don't have an answer for this simply because I lack of experience. We can certainly start with the attributes set in the main instance and one level up in the inheritance chain.<br><br></div><div>Other issues:<br></div><div>1. What if there are too many attributes? I don't think the number will explode beyond 30. I choose this number out of thin air. I can do more research on this. It doesn't actually hurt to see everything. If you do have a class with so many attribute (whether you have this many to begin with, or because you allow aritbary numbers of attributes to be set -- for example, a document from a collection in NoSQL like MongoDB), that's still very useful. We could limit by default up to how many.<br><br></div><div>2. How do we order them? We can order them in unsorted or sorted order. I prefer the sorted order.<br><br></div></div>