By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,293 Members | 1,397 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,293 IT Pros & Developers. It's quick & easy.

Odd unicode() behavior

P: n/a
The behavior of the unicode built-in function when given a unicode
string seems a little odd to me:
>>unicode(u"abc")
u'abc'
>>unicode(u"abc", "ascii")
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: decoding Unicode is not supported

I don't see why providing the encoding should make the function behave
differently when given a Unicode string. Surely unicode(s) ought to
bahave exactly the same as unicode(s,sys.getdefaultencoding())?

Any opinions?

Mike.

Aug 30 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
ma****@googlemail.com wrote:
The behavior of the unicode built-in function when given a unicode
string seems a little odd to me:
>>>unicode(u"abc")
u'abc'
>>>unicode(u"abc", "ascii")
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: decoding Unicode is not supported

I don't see why providing the encoding should make the function behave
differently when given a Unicode string. Surely unicode(s) ought to
bahave exactly the same as unicode(s,sys.getdefaultencoding())?
nope.

if you omit the encoding argument, unicode() behaves pretty much like str(),
using either the __unicode__ method or __str__/__repr__ + decoding to get
a Unicode string.

see the language reference for details, e.g:

http://pyref.infogami.com/unicode

</F>

Aug 30 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.