python variable type and immutable, deep copy vs shallow copy

Reprinted: https://www.cnblogs.com/huamingao/p/5809936.html

Core Tip:

The variable type Vs immutable type

The variable type (mutable): lists, dictionaries

Immutable type (unmutable): number, string, a tuple

Variable immutable herein, refers to whether the piece of content (value) may be changed in the memory

Code:

name1='wupeiqi'
name2=name1
print("name1:%s\nname2:%s" %(name1,name2))
name1='alex'
print("I have renamed name1 to new_name.Let's see what happens!") 
print("name1:%s\nname2:%s" %(name1,name2))

Results of the:

C:/Personal/OldboyPython/day01/test.py
name1:wupeiqi
name2:wupeiqi
I have renamed name1 to new_name.Let's see what happens!
name1:alex
name2:wupeiqi

Question: Why does not the value name2 name1 together and become alex? Here the answer yet, look at the chart and explain later.

 

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

Assignment of variables

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

name1 = "wupeiqi"
name2 = "alex"

# ! / Usr / bin / env Python 
# - * - Coding: UTF-8 - * - 

name1 = " wupeiqi "    
name2 = name1      # make name2 and name1 points assigned to the same object, just create a variable that points to the original memory address,

1. The reference count decrease

When the object wupeiqi (blue in FIG wupeiqi memory block) is first created, and (its reference) when assigned to a variable name1, the object wupeiqi reference count is set to 1.

When the object alex (blue in FIG alex memory block) is first created, and (its reference) assigned to the variable name2, the object alex reference count is set to 1.

When the variable name1 assigned to the variable name2 (name2 = name1), is actually the target wupeiqi assigned to NAME2, and therefore the object wupeiqi reference count automatically incremented by 1, and the object alex reference count decremented, i.e., reduced to zero, the trigger garbage collection mechanism.

2. Variable Type Vs immutable type

The variable type (mutable): lists, dictionaries

Immutable type (unmutable): number, string, a tuple

Variable immutable herein, refers to whether the piece of content (value) may be changed in memory. If it is immutable, at the time of operation of the object itself, you must apply for a new area in memory (because the old area # # immutable). If it is a variable type of object manipulation time, do not need to apply elsewhere in memory, just behind the continuous application of this object (+/-) can be, that is, its address will remain the same, but the area will longer or shorter.

You can use the built-in function id () to confirm the identity of the object before and after the change has occurred in two assignments. See examples may http://blog.chinaunix.net/uid-26249349-id-3080279.html

* What are the benefits immutable type? If the data is immutable, when we do not understand the data to a API, we can ensure that our data is not modified. If we want to operate from a tuple function returns, through built-in function list () to convert it into a list. (When asked about the difference between lists and tuples, we can say that!)

3. Vs shallow copy deep copy

copy.copy () shallow copy

copy.deepcopy () deep copy

Shallow copy creates a new type of object, like the original, but its content is a reference to the original object element. The copy of the object itself is new, but the content is not. Sequence type copying objects (list \ tuples), default shallow copy.

 

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

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

n4 = n3 = n2 = n1 = "123/'Wu'"

About assignment, look at a dictionary example:

n1 = {"k1": "wu", "k2": 123, "k3": ["alex", 678]}
n2 = n1

浅拷贝,在内存中只额外创建第一层数据,如下图

深拷贝,在内存中将所有的数据重新创建一份(排除最后一层,即:python内部对字符串和数字的优化),如下图:

import copy
n1 = {"k1": "wu", "k2": 123, "k3": ["alex", 456]}
n4 = copy.deepcopy(n1)

 

Guess you like

Origin www.cnblogs.com/shengguorui/p/11334270.html