5

Firstly, is there one?

If not, is there a nice way to force something like

print '%s' % obj

to call obj.__unicode__ instead of obj.__str__?

3 Answers 3

4

Just use a unicode format string, rather than having a byte string in that role:

>>> class X(object):
...   def __str__(self): return 'str'
...   def __unicode__(self): return u'unicode'
... 
>>> x = X()
>>> print u'%s' % x
unicode
Sign up to request clarification or add additional context in comments.

4 Comments

Having __unicode__ return a bytestring and printing a unicode object make awful examples.
@Mike, agreed on the return issue -- fixed that typo. Strong disagreement on the print: on a decent platform, sys.stdout.encoding is set and the print will work perfectly -- while you're guessing blindly at the some_encoding to use (if it's not on sys.stdout how are you gonna guess it, pray?).
on what "decent platform" does python -c 'print u"\xff"' >some_file not raise a UnicodeEncodeError?
@Aaron, if sys.stdout.encoding is None (or 'ascii', etc), which would be the case in your example, of course printing characters not renderable in that encoding is an error -- just like @Mike's code would be if the mysterious some_encoding he uses but never specifies was similarly non-universal (or a mismatch for sys.stdout.encoding, which it completely ignores).
1

No. It wouldn't make sense for this to be the case.

print (u"%s" % obj).encode(some_encoding) will use obj.__unicode__.

1 Comment

Hate the idea of blindly guessing at the some_encoding as this answer would force one to do -- a decent platform will set sys.stdout.encoding so a plain print will work perfetly, if yours doesn't you must wrap sys.stdout into a wrapper which sets the encoding, in one place (then you can use print as is only proper and decent!), rather than strewing such guesses all over the furniture (what a horrible way to program).
0

Firstly, is there one?

Sure (sort of). A unicode format string will convert the format values to unicode, implying that obj.__unicode__ will be called (reference).

u'this is a %s' % ('unicode string')

Aside from the above, there's no particular reason why you couldn't be explicit:

print '%s' % (unicode(obj))

6 Comments

print '%s' % unicode(obj) isn't really explicit. It implicitly upconverts a str into a unicode when the % applies, which is among the uglier things that Python does.
Stumbled across this a few seconds later. I guess this works for me!
...and it relies on printing a unicode string exactly like my much reviled, much-downvoted, and much-earlier answer -- seeing this one upvoted instead could confirm suspicion that the downvoters are in fact intent on a personal vendetta, not a technical objection, whatever mealy-mouthed words they may try to dress it up in;-).
@Alex - I didn't realize how close my answer was until after I posted it ... you may be right :)
@Mark, indeed I imagine you didn't, since I see you accepted it -- thanks!-)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.