str vs unicode

Ben Finney ben+bazaar at benfinney.id.au
Fri May 27 03:35:37 UTC 2011


Martin Pool <mbp at canonical.com> writes:

> To date we tend to use repr(o) for a debug-oriented representation of
> it, and str(o) for a user-oriented representation to put in messages
> etc.  This is not absolutely widely used but it does seem useful.

(I don't contribute to Bazaar's code base (due to community-hostile CLA)
so my comments on this should be read as that of an observer only.)

The above description – ‘repr(obj)’ for the object's programmer-helpful
representation, ‘str(obj)’/‘unicode(obj)’ for the object's user-helpful
representation – is what I understand to be a strong convention in
Python generally. I approve.

> In some cases it is better to have an explicit method to get the
> human-readable form, but just being able to fold objects into string
> templates is worthwhile.

I would think the “represent this object as text” is best implemented
*only* through one of the above methods, since those are TOOWTDI for
these purposes.

> So then the question is, should we perhaps instead move to using
> __unicode__ on objects for the latter, for the sake of being a bit
> safer with unicode values, and perhaps also for an easier transition
> to python3?

Yes, I'm encouraging this in projects I currently work on. It makes a
lot of sense.

-- 
 \         “If nature has made any one thing less susceptible than all |
  `\    others of exclusive property, it is the action of the thinking |
_o__)              power called an idea” —Thomas Jefferson, 1813-08-13 |
Ben Finney




More information about the bazaar mailing list