4

I cannot understand why the following type changes to unicode from str.

CASE1

Python 2.7 (r27:82500, Nov 19 2014, 18:07:42)
[GCC 4.5.1 20100924 (Red Hat 4.5.1-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> x = {'resources': {}, 'tags': ['a', 'b']}  
>>> ret = json.dumps( x )
>>> ret
'{"resources": {}, "tags": ["a", "b"]}'
>>>
>>> type( ret )
<type 'str'>
>>> ret2 = json.loads( ret )
>>> ret2
{'resources': {}, 'tags': ['a', 'b']}

CASE2

Python 2.7.5 (default, Apr 22 2015, 21:27:15) 
[GCC 4.9.2 20141101 (Red Hat 4.9.2-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> x = {'resources': {}, 'tags': ['a', 'b']}  
>>> ret = json.dumps( x )
>>> ret
'{"resources": {}, "tags": ["a", "b"]}'
>>> type( x )
<type 'dict'>
>>> type( ret )
<type 'str'>
>>> ret2 = json.loads( ret )
>>> ret2
{u'resources': {}, u'tags': [u'a', u'b']}
>>> 

So, in the case 2 we see unicode objects where as in case 1, we see string instead. I don't see any code change has happened in the two version of python that can lead to this. May be I missed something. Any leads would be appreciated. Thanks

1 Answer 1

6

Version 2.7 of Python had a bug that caused the behavior in your first example. This was fixed in 2.7.5. See issue 10038. Note that version 2.6.6 behaves like 2.7.5, indicating that the 2.7 behavior was a change from the previously-established behavior.

I don't think that any code change has happened in the two version of python that can lead to this.

No need to "think" nothing changed when you can check and be sure! Every release of Python comes with extensive notes indicating exactly what changed. The term "json" appears twenty-eight times in the Python 2.7.5 change log. Changes could also have been made to JSON in Python 2.7.1, 2.7.2, 2.7.3, and 2.7.4, of course.

Sign up to request clarification or add additional context in comments.

1 Comment

Actually , I meant, I don't see any relvant code changes in json.loads() function definition. May be I missed something.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.