Brief discussion on Python string storage form

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.