[Python-Dev] Printing and unicode (original) (raw)
Guido van Rossum guido@python.org
Tue, 12 Nov 2002 12:21:48 -0500
- Previous message: [Python-Dev] Printing and __unicode__
- Next message: [Python-Dev] Printing and __unicode__
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
If you try str before unicode, you'll always get the default str for all new-style classes.
B. A file indicates "unicode-awareness" somehow. For a Unicode-aware file, it tries unicode, str, and repr, in order.
I like this.
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.
Falling back to repr without str doesn't make sense.
Which of these, if any, would be most Pythonish?
B.
--Guido van Rossum (home page: http://www.python.org/~guido/)
- Previous message: [Python-Dev] Printing and __unicode__
- Next message: [Python-Dev] Printing and __unicode__
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]