inconvenient unicode conversion of non-string arguments

Holger Joukl Holger.Joukl at LBBW.de
Wed Dec 13 05:16:09 EST 2006


python-list-bounces+holger.joukl=lbbw.de at python.org schrieb am 13.12.2006
11:02:30:

>
> Holger Joukl wrote:
> > Hi there,
> >
> > I consider the behaviour of unicode() inconvenient wrt to conversion of
> > non-string
> > arguments.
> > While you can do:
> >
> > >>> unicode(17.3)
> > u'17.3'
> >
> > you cannot do:
> >
> > >>> unicode(17.3, 'ISO-8859-1', 'replace')
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in ?
> > TypeError: coercing to Unicode: need string or buffer, float found
> > >>>
> > [...]
> > Any reason why unicode() with a non-string argument should not allow
the
> > encoding and errors arguments?
>
> There is reason: encoding is a property of bytes, it is not applicable
> to other objects.

Ok, but I still don't see why these arguments shouldn't simply be silently
ignored
for non-string arguments.

> > Or some good solution to work around my problem?
>
> Do not put undecoded bytes in a mixed-type argument list. A rule of
> thumb working with unicode: decode as soon as possible, encode as late
> as possible.

It's not always that easy when you deal with a tree data structure with the
tree elements containing different data types and your user may decide to
output
root.element.subelement.whateverData.
I have the problems in a logging mechanism, and it would vanish if
unicode(<non-string>, encoding, errors) would work and just ignore the
obsolete
arguments.

Best regards,
Holger


Der Inhalt dieser E-Mail ist vertraulich. Falls Sie nicht der angegebene
Empfänger sind oder falls diese E-Mail irrtümlich an Sie adressiert wurde,
verständigen Sie bitte den Absender sofort und löschen Sie die E-Mail
sodann. Das unerlaubte Kopieren sowie die unbefugte Übermittlung sind nicht
gestattet. Die Sicherheit von Übermittlungen per E-Mail kann nicht
garantiert werden. Falls Sie eine Bestätigung wünschen, fordern Sie bitte
den Inhalt der E-Mail als Hardcopy an.

The contents of this  e-mail are confidential. If you are not the named
addressee or if this transmission has been addressed to you in error,
please notify the sender immediately and then delete this e-mail.  Any
unauthorized copying and transmission is forbidden. E-Mail transmission
cannot be guaranteed to be secure. If verification is required, please
request a hard copy version.





More information about the Python-list mailing list