unicode() vs. s.decode()

Michael Ströder michael at stroeder.com
Sat Aug 8 19:42:14 EDT 2009


Michael Fötsch wrote:
> If speed is your primary concern, this will give you even better
> performance than unicode():
> 
>   decoder = codecs.lookup("utf-8").decode
>   for i in xrange(1000000):
>       decoder("äöüÄÖÜß")[0]

Hmm, that could be interesting. I will give it a try.

> However, there's also a functional difference between unicode() and
> str.decode():
> 
> unicode() always raises an exception when you try to decode a unicode
> object. str.decode() will first try to encode a unicode object using the
> default encoding (usually "ascii"), which might or might not work.

Thanks for pointing that out. So in my case I'd consider that also a plus for
using unicode().

Ciao, Michael.



More information about the Python-list mailing list