This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author eric.araujo
Recipients eric.araujo
Date 2011-10-19.19:44:54
SpamBayes Score 1.43997e-10
Marked as misclassified No
Message-id <1319053495.26.0.624132637793.issue13224@psf.upfronthosting.co.za>
In-reply-to
Content
Suggestion by Guido on #868845:

Off-topic: I sometimes wish that the str() of a class would return the class name rather than its repr(); that way "print(str)" would print "str" instead of <class 'str'>.  (Use case: printing an exception and its message: I wish I could print("%s: %s" % (err.__class__, err)) instead of having to use err.__class__.__name__.)  One could even claim that the repr() of a class could be the same, since one of the guiding principles for repr() is that it should, if possible, return an expression that (perhaps given a suitable environment) could reconstruct the value exactly (and otherwise it should have the <...> form discussed in this issue).  But both of these wishes are debatable, and if anyone cares, they should open a new bug to discuss it.

> I sometimes wish that the str() of a class would return the class name
> rather than its repr(); that way "print(str)" would print "str"
> instead of <class 'str'>.  (Use case: printing an exception and its
> message: I wish I could print("%s: %s" % (err.__class__, err)) instead
> of having to use err.__class__.__name__.)

I wrote a simple patch for that.  I just copied the definition of type_repr to a new type_str function, edited the format strings and updated the member mapping (I checked in another file than casting to reprfunc is okay for a str func).  It compiles and runs just fine, but I’m still learning C, so there may be things I’ve missed: I don’t know if I have to declare the new function or something like that.  The test suite passes with very few edits.


Guido added this:
> One could even claim that the repr() of a class could be the same
I for one think of repr first as “string form for debugging”, so I like the angle brackets.  My patch leaves __repr__ alone.


If this get approved, I’ll update my patch with doc changes.  If there is no feedback I’ll go to python-ideas.
History
Date User Action Args
2011-10-19 19:45:43eric.araujounlinkissue13224 messages
2011-10-19 19:44:55eric.araujosetrecipients: + eric.araujo
2011-10-19 19:44:55eric.araujosetmessageid: <1319053495.26.0.624132637793.issue13224@psf.upfronthosting.co.za>
2011-10-19 19:44:54eric.araujolinkissue13224 messages
2011-10-19 19:44:54eric.araujocreate