Why string comparison and authentication behave differently in pdb and python console

I run the same python code snippet in the python and pdb console, but I get different results as below:

PDB:

>>> import pdb
>>> pdb.set_trace()
(Pdb) print u'你好' == u'\u4f60\u597d'
False
(Pdb) print u'你好' is u'\u4f60\u597d'
False
(Pdb) print id(u'你好'), id(u'\u4f60\u597d')
4431713024 4431713120
(Pdb) id(u'你好')
4431713024
(Pdb) id(u'\u4f60\u597d')
4431713024

python console:

>>> print u'你好' == u'\u4f60\u597d'
True
>>> print u'你好' is u'\u4f60\u597d'
True
>>> print id(u'你好'), id(u'\u4f60\u597d')
4376711984 4376711984
>>> id(u'你好')
4376711984
>>> id(u'\u4f60\u597d')
4376711984

My python version is 2.7.13

So my questions are:

1.why operators (for example, '==' and 'is') work differently in two consoles.

2. In pdb, id (u '\ u4f60 \ u597d') is 4431713120 in

print id(u'你好'), id(u'\u4f60\u597d')

but 4431713024 in

id(u'\u4f60\u597d')

3.Therefore, this situation does not occur in python3

+4
source share
1 answer

Let's start with checks isbecause it is a little easier to answer.

, id id . , - , id. , . , . , , . id, ( ).

id -, . . , id, . ( Python string interning, @DeepSpace , ).

, , , u'你好' == u'\u4f60\u597d'. - , ( , ).

:

(Pdb) map(ord, u'你好')
[228, 189, 160, 229, 165, 189]
(Pdb) map(ord, u'\u4f60\u597d')
[20320, 22909]

:

>>> map(ord, u'你好')
[20320, 22909]
>>> map(ord, u'\u4f60\u597d')
[20320, 22909]

, - .

+1

Source: https://habr.com/ru/post/1694177/


All Articles