title: Decoding if it's not unicode
body: <p>I want my function to take an argument that could be an unicode object or a utf-8 encoded string. Inside my function, I want to convert the argument to unicode. I have something like this:</p>
  
  <pre><code>def myfunction(text):
      if not isinstance(text, unicode):
          text = unicode(text, 'utf-8')
  
      ...
  </code></pre>
  
  <p>Is it possible to avoid the use of isinstance? I was looking for something more duck-typing friendly.</p>
  
  <p>During my experiments with decoding, I have run into several weird behaviours of Python. For instance:</p>
  
  <pre><code>&gt;&gt;&gt; u'hello'.decode('utf-8')
  u'hello'
  &gt;&gt;&gt; u'cer\xf3n'.decode('utf-8')
  Traceback (most recent call last):
    File "&lt;input&gt;", line 1, in &lt;module&gt;
    File "/usr/lib/python2.6/encodings/utf_8.py", line 16, in decode
      return codecs.utf_8_decode(input, errors, True)
  UnicodeEncodeError: 'ascii' codec can't encode character u'\xf3' in po
  sition 3: ordinal not in range(128)
  </code></pre>
  
  <p>Or</p>
  
  <pre><code>&gt;&gt;&gt; u'hello'.decode('utf-8')
  u'hello' 12:11
  &gt;&gt;&gt; unicode(u'hello', 'utf-8')
  Traceback (most recent call last):
  File "&lt;input&gt;", line 1, in &lt;module&gt;
  TypeError: decoding Unicode is not supported
  </code></pre>
  
  <p>By the way. I'm using Python 2.6</p>