Python string Storage Format
Http://blog.csdn.net/zhonghuan1992
Zhongkui
August 31, 2014
Python string Storage Format
Record your questions found today and give answers to your existing knowledge. To put it short, python users should be familiar with = and is, But I will introduce it here.
= Is used to determine whether two things are equal, for example:
a = 10;b = 10;print(a == b);
The output is true;
Let's look at another example:
a = [1,2,3];b = [1,2,3];c = [1,2,4];print(a==b);print(a==c);
The output values are true and false respectively;
Let's look at is; it is used to judge the same identity, not the same sex.
Let's look at the following example:
x = [1,2,3];y = [1,2,3];print(x is y);
The output is false;
And only:
x = [1,2];y = x;print(x is y);
The output is true;
From the above example, we can see the difference between = and is.
Now, if we replace the comparison with a string, will the result be unexpected. See the following example:
x = “huan”;y = x;print(x == y);print(x is y);
I think you can guess what the output is.
The output is true.
True
Now let's look at the following example:
x = “huan”;x = “huan”;print(x == y);print(x is y);
What are the results?
The output is true.
True
Why? It is understandable that x and y are equal, but why is x and y the same object? According to my guess, the python internal storage string adopts the Flyweight pattern mode. What is the metadata mode? You can refer to a blog I wrote a few days ago.