title: Python: How to force a "print" to use __unicode__ instead of __str__, or otherwise naturally "print" the message without explicitly calling unicode()
body: <p>Basically I just want to be able to create instances using a class called Bottle: eg <code>class Bottle(object):...</code>  and then in another module be able to simply "print" any instance <strong>without</strong> having to hack code to explicitly call a character encoding routine.</p>
  
  <p>In summary, when I try:</p>
  
  <pre><code>obj=Bottle(u"味精")
  print obj
  </code></pre>
  
  <p>Or to an "in place" "print":</p>
  
  <pre><code>print Bottle(u"味精")
  </code></pre>
  
  <p>I get:</p>
  
  <pre><code>"UnicodeEncodeError: 'ascii' codec can't encode characters"
  </code></pre>
  
  <p>Similar stackoverflow questions:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/2189156/unicode-class-in-python">unicode class in Python</a></li>
  <li><a href="https://stackoverflow.com/questions/2688020/how-to-print-chinese-word-in-my-code-using-python">how to print chinese word in my code.. using python</a></li>
  <li><a href="https://stackoverflow.com/questions/2389410/python-string-decoding-issue">Python string decoding issue</a></li>
  <li><a href="https://stackoverflow.com/questions/507123/python-3-0-how-to-make-print-output-unicode">python 3.0, how to make print() output unicode?</a></li>
  </ul>
  
  <p>¢ It's currently not feasible to switch to python3. ¢</p>
  
  <p>A solution or hint (and explanation) on how to do an in place utf-8 print (just like class U does <strong>successfully</strong> below) would be muchly appreciated. :-)</p>
  
  <p>ThanX N</p>
  
  <p>--</p>
  
  <p>Sample code:</p>
  
  <p>-------- 8>&lt; - - - -  cut here - - - -</p>
  
  <pre><code>#!/usr/bin/env python
  # -*- coding: utf-8 -*-
  
  def setdefaultencoding(encoding="utf-8"):
    import sys, codecs
  
    org_encoding = sys.getdefaultencoding()
    if org_encoding == "ascii": # not good enough
      print "encoding set to "+encoding
      sys.stdout = codecs.getwriter(encoding)(sys.stdout)
      sys.stderr = codecs.getwriter(encoding)(sys.stderr)
  
  setdefaultencoding()
  
  msg=u"味精" # the message!
  
  class U(unicode): pass
  
  m1=U(msg)
  
  print "A)", m1 # works fine, even with unicode, but
  
  class Bottle(object):
    def __init__(self,msg): self.msg=msg
    def __repr__(self): 
      print "debug: __repr__",self.msg
      return '{{{'+self.msg+'}}}'
    def __unicode__(self): 
      print "debug: __unicode__",self.msg
      return '{{{'+self.msg+'}}}'
    def __str__(self): 
      print "debug: __str__",self.msg
      return '{{{'+self.msg+'}}}'
    def decode(self,arg): print "debug: decode",self.msg
    def encode(self,arg): print "debug: encode",self.msg
    def translate(self,arg): print "debug: translate",self.msg
  
  m2=Bottle(msg)
  
  #print "B)", str(m2)
  print "C) repr(x):", repr(m2)
  print "D) unicode(x):", unicode(m2)
  print "E)",m2 # gives:  UnicodeEncodeError: 'ascii' codec can't encode characters
  </code></pre>
  
  <p>-------- 8>&lt; - - - -  cut here - - - -
  Python 2.4 output:</p>
  
  <pre><code>encoding set to utf-8
  A) 味精
  C) repr(x): debug: __repr__ 味精
  {{{\u5473\u7cbe}}}
  D) unicode(x): debug: __unicode__ 味精
  {{{味精}}}
  E) debug: __str__ 味精
  Traceback (most recent call last):
    File "./uc.py", line 43, in ?
      print "E)",m2 # gives:  UnicodeEncodeError: 'ascii' codec can't encode characters
  UnicodeEncodeError: 'ascii' codec can't encode characters in position 3-4: ordinal not in range(128)
  </code></pre>
  
  <p>-------- 8>&lt; - - - -  cut here - - - -
  Python 2.6 output:</p>
  
  <pre><code>encoding set to utf-8
  A) 味精
  C) repr(x): debug: __repr__ 味精
  Traceback (most recent call last):
    File "./uc.py", line 41, in &lt;module&gt;
      print "C) repr(x):", repr(m2)
  UnicodeEncodeError: 'ascii' codec can't encode characters in position 3-4: ordinal not in range(128)
  </code></pre>