[Python-Dev] Printing and unicode (original) (raw)

Martin v. Loewis martin@v.loewis.de
12 Nov 2002 16:02:39 +0100


In c.l.p, Henry Thompson wondered why printing would ignore unicode. Consider this:

import codecs stream = codecs.open("/tmp/bla","w", encoding="cp1252")

class Foo: def unicode(self): return u"\N{EURO SIGN}"

foo = Foo() print >>stream, foo

This succeeds, but /tmp/bla now contains

<__main__.Foo instance at 0x4026e68c>

He argues that it instead should invoke unicode, similar to invoking automatically str when writing to a byte stream.

I agree that this is desirable, but I wonder what the best approach would be:

A. Printing tries str, unicode, and repr, in this order.

B. A file indicates "unicode-awareness" somehow. For a Unicode-aware file, it tries unicode, str, and repr, in order.

C. A file indicates that it is "unicode-requiring" somehow. For a unicode-requiring file, it tries unicode; if that fails, it tries repr and converts the result to Unicode.

Which of these, if any, would be most Pythonish?

Regards, Martin