0

If I have an unknown object X with some method of getting a string representation (__unicode__, __str__, __repr__, etc.).

How can I safely convert that object to a unicode string without ever having to worry about seeing any of those ugly unicode encode/decode errors?

TheOne
  • 10,819
  • 20
  • 81
  • 119
  • By using `unknown object` in you program you ask for trouble. I don't know what you mean. Normally you should know the tools you're using. Especially the encoding of incoming data. – ElmoVanKielmo Mar 27 '14 at 20:30
  • 1
    Suppose your unknown object is a bytestring. It has `__str__`, `__repr__` methods (but no `__unicode__`). Without knowing its character encoding, you can't decode it into Unicode text unambiguously and without losing information in the general case. [There Ain't No Such Thing As Plain Text](http://www.joelonsoftware.com/articles/Unicode.html). – jfs Mar 27 '14 at 20:37

0 Answers0