Different positions for storing identical lines with special characters

I am new to python and now I am exploring some of its core functionality.

Could you explain to me why the following example always returns false in the case of a string with special characters:

>>> a="x" >>> b="x" >>> a is b True >>> a="xxx" >>> b="xxx" >>> a is b True >>> a="xü" >>> b="xü" >>> a is b False >>> a="ü" >>> b="ü" >>> a is b True >>> #strange: with one special character it works as expected 

I understand that storage positions are different for strings with special characters for each assignment, I already checked it with the id () function, but for what reason does python process strings in this inconsistent way?

+6
source share
2 answers

Python (at least the reference implementation) has a cache for small integers and strings. I think unicode strings outside the ASCII range are larger than the cache threshold (internal Unicode is stored using 16 or 32-bit characters, UCS-2 or UCS-4 ) and therefore they are not cached.

[edit]

Found a more complete answer: About changing the identifier of an immutable Python string

Se also: http://www.laurentluce.com/posts/python-string-objects-implementation/

+3
source

C: you do not check the equality between the lines, you check the equality between the objects that are resolved using pointers. So your code is:

 >>> a="x" >>> b="x" >>> a is b True 

does not ask: "a and b are the same character?", his query "are a and b the same object?". Since there is a small cache of objects (for small integers and one byte string, as mentioned earlier), the answer is “yes, both variables refer to the same object in memory, a small object x characters”.

When you work with an object that is not suitable for the cache, as in:

 >>> a="xü" >>> b="xü" >>> a is b False 

what happens is that a and b now refer to different objects in memory, so the operator allows false (a and b do not point to the same object!).

If an idea compares strings, you should use the == operator, not.

0
source

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


All Articles