[Python-ideas] about repr
spir
denis.spir at free.fr
Sun Apr 5 19:43:46 CEST 2009
Hello,
-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.
This seems easier for python 3: repr output would then be implemented as a function, like print, so that we wouldn't not need a dedicated keyword.
-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
You will probably find it weird, but I would really like a way to have a variable name automatically written together with its value. I can see 2 ways for that:
~ Either repr is really considered as programmer test output. Then when its argument is a simple name instead of another kind of expression, this name would be printed to stdout together with the value. (*)
~ Or a brand new pair of functions/methods: one to produce the string, and one to print it out.
(*) This special casing of identifier alone, as opposed to general expression, may seem strange; but it already exists in python assignments in order to alias instead of yielding a new value.
Denis
PS:
I cannot simply write a tool func, for ordinary objects do not know their own name: only functions, methods and classes have a __name__. I can nevertheless simulate it using eval():
def show(name):
sys.stdout.write( "%s:%r" % (name, eval(name)) )
but then show() must be called with "show('x')", using additional ''. Also, this cannot work as a method, so cannot be specialized.
What I wish is instead:
def show(obj):
sys.stdout.write( "%s:%r" % (obj.__name__, obj) )
that can be called with "show(x)". Why do only funcs & types know their names?
------
la vita e estrany
More information about the Python-ideas
mailing list