unicode question

wolfgang haefelinger wh2005 at web.de
Mon Nov 22 03:04:08 EST 2004


Hi Martin,

if print is implemented like this then I begin to understand the problem.

Neverthelss, I regard

 print y.__str__()            ## works
 print y                           ## fails??

as a very inconsistent behaviour.

Somehow I have the feeling that Python should give up the distinction
between unicode  and  str  and just have a str type which is internally
unicode.


Anyway, thanks for answering
Wolfgang.

""Martin v. Löwis"" <martin at v.loewis.de> wrote in message 
news:41a0ab62$0$151$9b622d9e at news.freenet.de...
> wolfgang haefelinger wrote:
>> I was actually thinking that
>>
>>  print x
>>
>> is just kind of shortcur for writing (simplifying bit):
>>
>>  import sys
>>  if not (isinstance(x,str) or isinstance(x,unicode)) and x.__str__ :
>>     x = x.__str__()
>>  sys.stdout.write(x)
>
> This is too simplifying. For the context of this discussion,
> it is rather
>
> import sys
> if isinstance(x, unicode) and sys.stdout.encoding:
>     x = x.encode(sys.stdout.encoding)
> x = str(x)
> sys.stdout.write(x)
>
> (this, of course, is still quite simplicated. It ignores tp_print,
> and it ignores softspaces).
>
>> Or in words: if x is not a string type but has method __str__ then
>>
>>  print x
>>
>> behaves like
>>
>>  print x.__str__()
>
> No. There are many types for which this is not true; in this specific
> case, it isn't true for Unicode objects.
>
>> Is this a bug??
>
> No. You are just misunderstanding it.
>
> Regards,
> Martin 





More information about the Python-list mailing list