Python string Storage Format

Source: Internet
Author: User

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.


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.