unicode question

"Martin v. Löwis" martin at v.loewis.de
Sun Nov 21 04:37:37 EST 2004


Kent Johnson wrote:
> Martin v. Löwis wrote:
> 
>> wolfgang haefelinger wrote:
>>
>>> I wonder especially about case 2. I can see that "print y" makes a 
>>> call to
>>> Y.__str__() . But Y.__str__() can be printed?? So what is 'print' 
>>> exactly doing?
>>
>>
>>
>> It looks at sys.stdout.encoding. If this is set, and the thing to print
>> is a unicode string, it converts it to the stream encoding, and prints
>> the result of the conversion.
> 
> 
> I hate to contradict an expert, but ISTM that it is 
> sys.getdefaultencoding() ('ascii') that is the problem, not 
> sys.stdout.encoding ('cp437')

It seems we were answering different parts of the question. I answered
the part "What is 'print' exactly doing"; you answered the part as to
what the problem with str() conversion is (although I'm not sure whether
the OP has actually asked that question).

Also, the one case that is interesting here was not in your experiment:
try

print gamma

This should work, regardless of sys.getdefaultencoding(), as long as
sys.stdout.encoding supports the characters to be printed.

Regards,
Martin



More information about the Python-list mailing list