[Python-ideas] about repr

Greg Ewing greg.ewing at canterbury.ac.nz
Mon Apr 6 03:02:43 CEST 2009


spir wrote:

> 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 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.

> 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. 

The way I like to characterize it is:

* str() is for normal output
* repr() is for debugging output

> 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 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.

> (*) 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.

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




More information about the Python-ideas mailing list