Python mutable types and immutable types

Source: Internet
Author: User

Original address: http://www.cnblogs.com/huamingao/p/5809936.html

mutable type Vs Immutable type

mutable type (mutable): list, dictionary

Immutable types (unmutable): Numbers, strings, tuples

The immutable variable here refers to whether the contents of the memory (value) can be changed

Code:

name1='Wupeiqi'name2=name1Print("name1:%s\nname2:%s"%(name1,name2)) name1='Alex'Print("I have assigned new value to name1. Let's see how happens to name2!")Print("name1:%s\nname2:%s"% (name1,name2))

Execution Result:

Name1:wupeiqiname2:wupeiqiI has renamed Name1 to New_name. Let's see whathappens! Name1:alexname2:wupeiqi

Question: Why didn't the value of name2 change to Alex with name1? Let's start by looking at the diagram and then explaining.

The following references are from http://www.cnblogs.com/wupeiqi/articles/5433925.html

Assigning values to variables

# !/usr/bin/env python # -*-coding:utf-8-*-  " Wupeiqi " "Alex"

# !/usr/bin/env python # -*-coding:utf-8-*-  "Wupeiqi"=   name1     #  causes name2 and name1 to point to the same object assignment, Simply create a variable that points to the original memory address,

The above is quoted from http://www.cnblogs.com/wupeiqi/articles/5433925.html

1. Increase or decrease of reference count

The reference count of the object Wupeiqi is set to 1 when the object Wupeiqi (the blue memory chunk Wupeiqi in the figure) is first created and assigned to the variable name1 (referencing).

When the object Alex (the blue memory chunk in the figure Alex) was first created and assigned to the variable name2, the object Alex's reference count is set to 1.

When the variable name1 is assigned to the variable name2 (name2=name1), the object Wupeiqi is actually assigned to name2, so the reference count of the object Wupeiqi automatically adds 1, and the object Alex's reference count automatically minus 1, that is, minus 0, triggering a garbage collection mechanism.

2. Variable type Vs immutable type

mutable type (mutable): list, dictionary

Immutable types (unmutable): Numbers, strings, tuples

The immutable variable here refers to whether the contents of the memory (value) can be changed. In the case of an immutable type, when manipulating the object itself, a new area must be requested in memory (because the old region # is immutable #). In the case of a mutable type, when an object is manipulated, it is not necessary to apply the memory elsewhere, just a continuous request (+/-) After this object, i.e. its address will remain unchanged, but the area will grow or become shorter.

You can use the built-in function ID () to confirm whether an object's identity has changed before and after two assignments. Examples can be see http://blog.chinaunix.net/uid-26249349-id-3080279.html

* What are the benefits of immutable types? If the data is immutable, when we pass the data to an API that we do not understand, we can ensure that our data is not modified. If we want to manipulate a tuple returned from a function, it can be converted to a list by the built-in function list (). (When asked about the difference between a list and a tuple, you can say this!) )

3. Deep copy Vs Shallow copy

Copy.copy () Shallow copy

Copy.deepcopy () deep copy

A shallow copy is a new creation of the same type as the original object, but its content is a reference to the original object element. This copy of the object itself is new, but the content is not. When copying a Sequence type object (list \ Tuple), the default is a shallow copy.

The following references are from http://www.cnblogs.com/wupeiqi/articles/5433925.html

assignment , just create a variable that points to the original memory address, as in the following example:

" 123/' Wu ' "

For an assignment, look at a dictionary example:

N1 = {"K1""Wu""K2""  K3": ["Alex", 456= N1

Shallow copy , creating only the first layer of data in memory, such as

Import= {"K1""wu""K2 " " K3 ": ["Alex", 456= copy.copy (n1)

deep copy , recreate all the data in memory (excluding the last layer, i.e., Python's internal optimization of strings and numbers), such as:

Import= {"K1""wu""K2  "K3": ["Alex", 456 = Copy.deepcopy (N1)

Python mutable types and immutable types

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.