
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

spir schrieb:
Use ``from sys import displayhook as show`` ;)
Use ``from pprint import pprint; pprint(vars())``. Georg -- 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.

spir wrote:
I don't think I would use it much. Usually when I want to print the repr of something, it's mixed in with other things that I *don't* want the repr of, e.g. print "Foo =", repr(foo) I want the string printed as a plain string, not as a repr.
The way I like to characterize it is: * str() is for normal output * repr() is for debugging output
I understand your motivation, but this would require more than a function, it would need special syntax. That's a very big thing to ask for.
Not sure what you mean by that. If you think that in a = b the expression 'b' is somehow treated specially because it's a bare name, you're wrong. The right hand side of an assignment is treated just like any other expression. -- Greg

spir schrieb:
Use ``from sys import displayhook as show`` ;)
Use ``from pprint import pprint; pprint(vars())``. Georg -- 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.

spir wrote:
I don't think I would use it much. Usually when I want to print the repr of something, it's mixed in with other things that I *don't* want the repr of, e.g. print "Foo =", repr(foo) I want the string printed as a plain string, not as a repr.
The way I like to characterize it is: * str() is for normal output * repr() is for debugging output
I understand your motivation, but this would require more than a function, it would need special syntax. That's a very big thing to ask for.
Not sure what you mean by that. If you think that in a = b the expression 'b' is somehow treated specially because it's a bare name, you're wrong. The right hand side of an assignment is treated just like any other expression. -- Greg
participants (3)
-
Georg Brandl
-
Greg Ewing
-
spir