__repr__ policy problem: should module be prefixed to output?
Donn Cave
donn at u.washington.edu
Tue Dec 19 12:56:16 EST 2000
Quoth scarblac at pino.selwerd.nl (Remco Gerlich):
| A.M. Kuchling <amk at mira.erols.com> wrote in comp.lang.python:
...
|> Frankly, the only place I ever use repr() is in debugging print
|> statements and log files, where I need to be very clear about what an
|> object is and avoid confusing the integer 1 with the string '1'. I
|> therefore write __repr__ on classes to be as useful for debugging
|> purposes as possible; it doesn't recurse into subobjects, but does
|> include the class name, the id() of the object, and any other unique
|> identifier for the object (an ID number, user name, whatever).
|>
|> To put my money where my mouth is, I'd like to file a documentation
|> bug about this. What bits of the Python documentation promote this
|> view of repr()? One is section 3 of the reference guide, in its
|> description of the __repr__ special method name; any others?
|
| What is the bug, precisely? You never use it?
The bug is that some documentation promotes a meaning for repr()
that is neither generally valid nor practical.
| The language reference has it when in the description of __repr__, and
| the library reference in the description of repr().
|
| This is only really an issue with instances, since you get to make your
| own __repr__ for those. And in my experience, it is usually quite possible
| to return a string that could be used to build the object again (for instance,
| by giving the right arguments to the constructor). Of course, if you
| have objects with lots of subobjects and so on, it may not be feasible
| and you don't use it.
|
| But I still like the rule. And, well, it's what repr does.
That's the problem, though, it is not what repr does, generally. What
use is a rule that has never been and will not be generally observed?
Python class instances in the library, C objects like compiled regexps.
>From the implementations that come with the distribution, it's pretty
clear to me that repr serves the purpose described above, a programmer's
printout, and that's an essential function too.
Donn Cave, donn at u.washington.edu
More information about the Python-list
mailing list