Chris's Wiki :: blog/python/GivingClassesAStr Commentshttps://utcc.utoronto.ca/~cks/space/blog/python/GivingClassesAStr?atomcommentsDWiki2018-10-28T03:54:24ZRecent comments in Chris's Wiki :: blog/python/GivingClassesAStr.By Tom on /blog/python/GivingClassesAStrtag:CSpace:blog/python/GivingClassesAStr:9ace103161f1e8bd1abb0362d3d2d2e85f5dff68Tom<div class="wikitext"><p>You can do this dynamically quite easily.</p>
<pre>
def __str__(self):
return str(self.__dict__)
</pre>
<p>Every object in python can be turned into a dict, because that's how they are stored. This will return a string with all the fields of the class. If you want it nicely formatted, use pprint.pformat.</p>
</div>2018-10-28T03:54:24ZBy Clément on /blog/python/GivingClassesAStrtag:CSpace:blog/python/GivingClassesAStr:5aecf2680986108be472e547943167838802bddeClément<div class="wikitext"><p>You should probably use a __repr__ for this type of debugging info. Additionally, attrs <em>is</em> in the stdlib now; or, more precisely, the new dataclass module offers most of the same functionality with a slightly different interface (<a href="https://docs.python.org/3/library/dataclasses.html">https://docs.python.org/3/library/dataclasses.html</a>).</p>
</div>2018-10-26T05:28:37Z