http://blog.csdn.net/zhonghuan1992
Zhonghuanlin
August 31, 2014
brief discussion on Python string storage form
Keep a record of what you find in your day and give answers to your existing knowledge. Long story short, people who have used python are not unfamiliar with = = and is, but I'll introduce you here.
= = is used to determine whether two things are equal, such as:
A = 10;b = 10;print (A = = B);
The output is true;
Let's look at an example:
A = [1,2,3];b = [1,2,3];C = [1,2,4];p rint (a==b);p rint (a==c);
The output is true and false, respectively;
To look at is, is to judge identity rather than similarity.
Take a look at the following example:
x = [1,2,3];y = [+/-];p rint (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 two.
Now that the question is coming, if we change the contrast to a string, will the result be unexpected to you? Look at the following example:
x = "Huan"; y = x;print (x = = y);p rint (x is y);
I think we can guess what the output is.
Output is: true
True
Now look at the following example:
x = "Huan"; x = "Huan";p rint (x = = y);p rint (x is y);
guess what it turned out to be?
Output is: true
True
Why is that? X and Y are equally understandable, but why are X and y the same object? As far as I'm guessing, Python's internal storage string, using the Flyweightpattern, What is the enjoy meta-mode , you can see my previous days written a blog,/http/ blog.csdn.net/zhonghuan1992/article/details/38856591, this is my current understanding, if you have other views, please leave your opinion we discuss.
Brief discussion on Python string storage form