[Python-Dev] Unicode debate

Fredrik Lundh Fredrik Lundh" <effbot@telia.com
Thu, 4 May 2000 09:46:05 +0200


Ka-Ping Yee <ping@lfw.org> wrote:
> I know this is not a small change, but i'm pretty convinced the
> right answer here is that the print hook should call a *method*
> on sys.stdout, whatever sys.stdout happens to be.  The details
> are described in the other long message i wrote ("Printing objects
> on files").
>=20
> Here is an addendum that might actually make that proposal
> feasible enough (compatibility-wise) to fly in the short term:
>=20
>     print x
>=20
> does, conceptually:
>=20
>     try:
>         sys.stdout.printout(x)
>     except AttributeError:
>         sys.stdout.write(str(x))
>         sys.stdout.write("\n")
>=20
> The rest can then be added, and the change in 'print x' will
> work nicely for any file objects, but will not break on file-like
> substitutes that don't define a 'printout' method.

another approach is (simplified):

    try:
        sys.stdout.write(x.encode(sys.stdout.encoding))
    except AttributeError:
        sys.stdout.write(str(x))

or, if str is changed to return any kind of string:

    x =3D str(x)
    try:
        x =3D x.encode(sys.stdout.encoding)
    except AttributeError:
        pass
    sys.stdout.write(x)

</F>