[Python-Dev] Printing and __unicode__

Guido van Rossum guido@python.org
Tue, 12 Nov 2002 12:21:48 -0500


> 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/)