[Python-ideas] about repr
g.brandl at gmx.net
Sun Apr 5 19:57:37 CEST 2009
> -1- repr writing function
> Wonder if you would like a statement (py2) or function (py3) similar to print, except that it would output the repr form instead of str. I would enjoy having such a nicety to avoid "print repr(x)" or "print(repr(x))".
> As a side note, there is imo something missing in the // below:
> __str__ str() %s print
> __repr__ repr() %r (show?)
> If only for consistency... it wouldn't hurt.
Use ``from sys import displayhook as show`` ;)
> -2- use for test purpose
> I just read the following by Ka-Ping Yee on python-dev (http://mail.python.org/pipermail/python-dev/2000-April/003238.html):
> repr() is for the human, not for the machine. [Serialization]
> is for the machine. repr() is: "Please show me as much information
> as you reasonably can about this object in an accurate and unambiguous
> way, but if you can't readably show me everything, make it obvious
> that you're not."
> I see str() as intended to produce a view of an object that has a kind of "natural" (read: cultural) textual form, like a date, and rather for the user. repr() instead seems to me more "rough", informative, and programmer-oriented.
> For this reason, I heavily use repr for test, usually have a __repr__ method on custom classes only for that purpose.
> I would like to know how much debug work is done without help of a debugger. I suspect it's a rather high percentage for several reasons, especially with a clear and friendly language like python. At test/debug/setup time it's often enough to have some variable output at the proper time & place. But we often need several values at several points.
> The one thing missing is then the names of the variables showed. So that we have to insert eg
> print "x:%r" % x
Use ``from pprint import pprint; pprint(vars())``.
Thus spake the Lord: Thou shalt indent with four spaces. No more, no less.
Four shall be the number of spaces thou shalt indent, and the number of thy
indenting shall be four. Eight shalt thou not indent, nor either indent thou
two, excepting that thou then proceed to four. Tabs are right out.
More information about the Python-ideas