[Python-Dev] Printing and __unicode__
Wed, 13 Nov 2002 10:24:34 +0100
Martin v. Loewis wrote:
> Guido van Rossum <firstname.lastname@example.org> writes:
>>>B. A file indicates "unicode-awareness" somehow. For a Unicode-aware
>>> file, it tries __unicode__, __str__, and __repr__, in order.
>>I like this.
> Ok, then the question is: How can a file indicate its
> unicode-awareness? I propose that presence of an attribute "encoding"
> is taken as such an indication; this would cover all existing cases
> with no change to the file-like objects.
Thanks to the time machine, this attribute is already available
on stream objects created with codecs.open().
> In case the stream is "natively" Unicode (i.e. doesn't ever convert to
> byte strings), setting encoding to None should be allowed (this
> actually indicates that StringIO should have the encoding attribute).
The presence of .encoding should indicate that it is
safe to write Unicode objects to .write(). Let the stream
decide what to do with the Unicode object (e.g. it would
probably encode the Unicode object using the .encoding
and only then write it to the outside world).
CEO eGenix.com Software GmbH
eGenix.com -- Makers of the Python mx Extensions: mxDateTime,mxODBC,...
Python Consulting: http://www.egenix.com/
Python Software: http://www.egenix.com/files/python/