__unicode__ vs. __str__: not quite parallel?

Martin v. Loewis martin at v.loewis.de
Tue Nov 12 08:37:01 EST 2002


ht at cogsci.ed.ac.uk (Henry S. Thompson) writes:

> Sorry I wasn't clearer, but I did say 'print', not 'write', and if
> you use print your example works fine:

[You also say "stream", which is a term that is not used often to
denote "file-like objects", so it was not expecting to interpret your
terminology literally.]

In any case, how would you expect this to work? If a thing is being
printed (i.e. as if being passed to a print statement), how should
print perform this processing?

Currently, it looks
1. whether it already is a string, and, if not,
2. whether it has an __str__, and, if not,
3. whether it has an __repr__.

In all cases, it ends up with a string, which it passes to the file's
.write method.

At what point would expect __unicode__ to come into play? I.e. how is
the print implementation supposed to know whether .write will accept
Unicode? And, if both __repr__ and __unicode__ are supported, would it
be better to invoke __unicode__ and get an exception, or would it be
better to invoke __repr__ and print the resulting string?

Regards,
Martin




More information about the Python-list mailing list