unicode question

"Martin v. Löwis" martin at v.loewis.de
Sun Nov 21 09:51:18 EST 2004


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