classification
Title: unicode(None) anomaly
Type: behavior Stage:
Components: Unicode Versions: Python 2.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: jgsack, loewis
Priority: normal Keywords:

Created on 2007-10-21 20:45 by jgsack, last changed 2007-10-22 06:28 by jgsack. This issue is now closed.

Messages (6)
msg56628 - (view) Author: James G. sack (jim) (jgsack) Date: 2007-10-21 20:45
'2.5 (r25:51908, Apr 10 2007, 10:27:40) \n[GCC 4.1.2 20070403 (Red Hat 
4.1.2-8)]'

unicode(None)
u'None'

This doesn't seem right, ;-)

Regards,
,,jim
msg56629 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2007-10-21 20:47
What answer did you expect instead?
msg56630 - (view) Author: James G. sack (jim) (jgsack) Date: 2007-10-21 21:12
(aside: Wow! that was a fast response to my posting!)

I'm not really sure what makes the most sense,
possibly:
 - an exception
 - u''
 - None

but not u'None'; not a string of length 4. That's quite unexpected!

Regards,
..jim

PS: I don't understand why I couldn't reply via email. I got an autoresponse

  You are not a registered user. Please register at:

http://bugs.python.org/user?@template=register

...before sending mail to the tracker.

Unknown address: jgsack...

I tried the registration, but that failed. I thought I _was_ registered.

Oh, well. :-[
..jim
msg56634 - (view) Author: James G. sack (jim) (jgsack) Date: 2007-10-22 01:03
Here's more:

>>> unicode(object)
u"<type 'object'>"

There seems to be an call to repr() somewhere in the process.

This seems, at least to me, to violate the principle of least surprise, and 
I'm thinking that unicode(x) ought to return UnicodeDecodeError if x is not 
a string. 

Maybe this is argueable. If so, I'd like to be educated. :-)

Regards,
..jim
msg56640 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2007-10-22 06:03
Ok. This is not a bug, but by design. unicode(X)==unicode(str(X)) for
most things, and str(X)==repr(X) for most things. repr(None)=='None',
hence the result you see. Closing as invalid.

P.S. To respond via email, you have to add your email address to Your
Details (you need to add all email addresses you want to use as From
addresses).
msg56641 - (view) Author: James G. sack (jim) (jgsack) Date: 2007-10-22 06:28
Martin v. Löwis wrote:
> Martin v. Löwis added the comment:
> 
> Ok. This is not a bug, but by design. unicode(X)==unicode(str(X)) for
> most things, and str(X)==repr(X) for most things. repr(None)=='None',
> hence the result you see. Closing as invalid.
> 
> P.S. To respond via email, you have to add your email address to Your
> Details (you need to add all email addresses you want to use as From
> addresses).
> 
> ----------
> resolution:  -> invalid
> status: open -> closed
> 
> __________________________________
> Tracker <report@bugs.python.org>
> <http://bugs.python.org/issue1308>
> __________________________________
> 

OK, thanks Martin.

Now that I think about it, I do understand.

My difficulty (and source of my surprise) was in mistakenly thinking of
unicode() as performing a string translation operation (that is, an
operation on strings).

I would now say that it is better thought of as a representation
function quite parallel to the str() function.

With that mindset, there is no surprise.

Thanks again, for your prompt attention and useful replies.

Regards,
..jim
History
Date User Action Args
2007-10-22 06:28:39jgsacksetmessages: + msg56641
2007-10-22 06:03:09loewissetstatus: open -> closed
resolution: not a bug
messages: + msg56640
2007-10-22 01:03:24jgsacksetmessages: + msg56634
2007-10-21 21:12:53jgsacksetmessages: + msg56630
2007-10-21 20:47:13loewissetnosy: + loewis
messages: + msg56629
2007-10-21 20:45:10jgsackcreate