[issue637094] print to unicode stream should __unicode

Marc-Andre Lemburg report at bugs.python.org
Thu Feb 12 15:01:22 CET 2009


Marc-Andre Lemburg <mal at egenix.com> added the comment:

On 2009-02-12 14:22, STINNER Victor wrote:
> STINNER Victor <victor.stinner at haypocalc.com> added the comment:
> 
>> Python3 fixes the "print" statement to be a function, which allows
>> much better extensibility of the concept.
>>
>> You can have the same in Python2 with a little effort: just create
>> your own myprint() function and have it process Unicode in whatever
>> way you want.
> 
> About myprint(): sure, it's possible to write a custom issue. But the feature 
> request was to use obj.__unicode__() instead of obj.__str__() for an "unicode 
> aware stream".
> 
> The problem with Python2 is that there is not clear separation between "bytes 
> stream" ("raw stream"?) and "unicode aware stream" (like io.open or 
> codecs.open). I think that fixing this issue (#637094) was one of the goal of 
> the new I/O library (io in Python3).

True, but the point of the original request was that the stream
should decide how to print the object, ie. you pass the object to
the stream's .write() method instead of first running str() on
it and then passing this to the .write() method.

This is easy to have using a custom print function and indeed
a good way to proceed if you want to port to Python3 at some
later point.

> Using __unicode__() for some object and __str__() for some other sounds 
> strange/dangerous to me. I prefer bytes-only stream or unicode-only stream, 
> but not bytes-and-sometimes-unicode stream.

If you use a StreamWriter instance in Python2 which uses one of
the Unicode codecs, then it will accept ASCII strings or Unicode
as input for .write(), ie. the stream decides on how to process
the input.

> Python2 has only bytes stream. Python3 has both: open(name, 'rb') is bytes 
> only, and open(name, 'r') is unicode only.

That's not entirely correct. Python2's codecs.py module provides
streams which can implement several different type combinations
for input and output.

> lemburg> Does your message mean that you want to reopen the issue?

No, I just wanted to correct your statement :-)

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue637094>
_______________________________________


More information about the Python-bugs-list mailing list