Summary: Python assignment, deep copy and shallow copy What is the difference python assignment, deep and shallow copy copy difference

Self-summary:

1) Assignment: Object assignment is actually a reference to the object.

In Python, a variable is a kind of address representation, does not open up open space.

2) as a pale copies: copy only the top layer (the first layer), no copy of the child object. So change the original data sub-objects, the child objects will change.

3) deep copy: shallow copy copy only the top layer is different from a reference copy of the deep hierarchy to copy, the copy until all references are immutable reference date.

 

 

Why Python default copy mode is shallow copy?

 

Angle time: time spent less shallow copy;

 

Spatial angle: shallow copy takes less memory;

 

Efficiency angle: shallow copy copy only the top-level data, the efficiency is higher than under normal circumstances a deep copy.

 

1. Assignment of the python, deep and shallow copy copy distinction

In python, the object assignment is actually a reference to an object. When you create an object and assign it to another variable of time, python and no copy of the object, but only a copy of this object.

(1) direct assignment, refers to the default shallow copy of the object passed it, changed the original list, the assigned b will do the same change

>>> b=alist
>>> print b
[1, 2, 3, ['a', 'b']]
>>> alist.append(5)
>>> print alist;print b
[1, 2, 3, ['a', 'b'], 5]
[1, 2, 3, ['a', 'b'], 5]

 

(2) copy a shallow copy, no copies of the child object, the original data is changed, the child object will change

>>> import copy

>>> c=copy.copy(alist)
>>> print alist;print c
[1, 2, 3, ['a', 'b']]
[1, 2, 3, ['a', 'b']]
>>> alist.append(5)
>>> print alist;print c
[1, 2, 3, ['a', 'b'], 5]
[1, 2, 3, ['a', 'b']]

Alist >>> [. 3]
[ 'A', 'B']
>>> alist [. 3] .append ( 'CCCC')
>>> Print alist; Print C
[. 1, 2,. 3, [ 'A', 'B', 'CCCC'],. 5]
[. 1, 2,. 3, [ 'a', 'B', 'CCCC']] inside the sub-object is changed

 

Deep copy, contains a copy of the object from the object inside, so changing the original object change will not cause a deep copy in any sub-elements

>>> import copy

D = copy.deepcopy >>> (alist)
>>> Print alist; Print D
[. 1, 2,. 3, [ 'A', 'B']]
[. 1, 2,. 3, [ 'A', 'B ']] has not changed
>>> alist.append (. 5)
>>> Print alist; Print D
[. 1, 2,. 3, [' A B '],. 5]', '
[. 1, 2,. 3, [ 'a', 'b'] ] has not changed
>>> alist [. 3]
[ 'A', 'B']
>>> alist [. 3] .append ( "CCCCC")
>>> Print alist; Print D
[. 1, 2,. 3, [ 'A', 'B', 'CCCCC'],. 5]
[. 1, 2,. 3, [ 'A', 'B']] has not changed

 

2. python reproduction, deep copy and shallow copy of the background and significance?

  Unlike matlab, like, for example, b = a, is to use a simple assignment to b, how they change after a, b will not change. c language is the same.

If you want to change how a, b on how change, c language proposed the concept of reference, the equivalent of an alias.

example:

int a; int & ra = a; // define references ra, which is a variable reference, i.e. aliases

! Okay Detailed This article has shallow copy and a deep copy of the essential reason: memory to store reopened

https://www.jianshu.com/p/9ed9b5ce7bb0

https://baijiahao.baidu.com/s?id=1627356407968660842&wfr=spider&for=pc 

This article presents the design pythond can prevent data tampering, or the flexibility to change

 

   In Python, there is a very popular view, all things are subject to the object. Say is that any type of data structure is an object, whether it is numbers, strings, or function, or even a module, Python are all as an object processing.

All Python objects have three properties: the identity, type, value.

  1.  
    name= "Li J"
  2.  
    print(type(name))
  3.  
    print(id(name))
  4.  
    print(name)
  5.  
     
  6.  
    # Output:
  7.  
    #<type 'str'>
  8.  
    #140334394101408
  9.  
    #Li J

     Variable and immutable objects

    In Python, by way of an update target, the object can be divided into two categories: variable immutable object.

   Mutable objects: lists, dictionaries, collections. The so-called variable refers to the variable value of the variable object identity is constant.

     Immutable objects: number, string, a tuple. Immutable object is the identity and value of the object is not variable. The newly created object is linked to the original variable names, old discarded objects, the garbage collector will reclaim these objects at the right time.

  1.  
    = var1 "Python" # string type is immutable
  2.  
    print(id(var1))
  3.  
    var1= "java"
  4.  
    print(id(var1))
  5.  
     
  6.  
    = A [ . 3, . 4] #list is variable,
  7.  
    print(id(a))
  8.  
    a.append( 3)
  9.  
    print(id(a))
  10.  
     
  11.  
    # Output:
  12.  
    140591145210096
  13.  
    140591145211632
  14.  
    140590909362688
  15.  
    140590909362688

 

Quote

     In the Python program, each object will apply to open up a space to hold the object in memory address location of the object in memory is called a reference. In developing the program, the variable names defined by reference to the actual address of the object.

     Refers to the actual number is a numeric address in memory, when using an object, as long as know the address of this object, you can manipulate the object, but because this number address and inconvenient to use in the development of memory, so use the form to the variable name Instead of objects numeric address. In Python, a variable is a kind of address representation, does not open up open space.

      Like the IP address, when you visit the site, are determined by the actual IP address of the host, and IP addresses are hard to remember, so use the domain name instead of the IP address when using the domain name to access the site, the domain name is resolved to an IP address to use .

By an example to illustrate the variables and variables point of reference is a thing:

  1.  
    b= 18
  2.  
    print(id(b))
  3.  
    print(id( 18))
  4.  
     
  5.  
    Output:
  6.  
    29413312
  7.  
    29413312

      

Shallow copy:

  1.  
    Print ( "shallow copy:")
  2.  
    import copy
  3.  
    b=[ 1,2,3,4,5]
  4.  
    print( "id b:",id(b))
  5.  
    h=copy.copy(b)
  6.  
    print( "id h",id(h))
  7.  
    print(h)
  8.  
    h.append( 6)
  9.  
    print(h)
  10.  
    print( "id h",id(h))
  11.  
    Print (b) # h shallow copy of the new list has changed, the original unchanged b.
  12.  
     
  13.  
    B [ . 1] = 'n-' after the change list elements #, the new list has not changed
  14.  
    print(h)
  15.  
     
  16.  
    Output:
  17.  
    Shallow copy:
  18.  
    ( 'id b:', 140165805110552)
  19.  
    ( 'id h', 140165805110480)
  20.  
    [ 1, 2, 3, 4, 5]
  21.  
    [ 1, 2, 3, 4, 5, 6]
  22.  
    ( 'id h', 140165805110480)
  23.  
    [ 1, 2, 3, 4, 5]
  24.  
    [ 1, 2, 3, 4, 5, 6]
  1.  
    a = [ 1, 2]
  2.  
    l1 = [ 3, 4, a]
  3.  
    l2 = copy.copy(l1)
  4.  
    print(l1)
  5.  
    print(l2)
  6.  
    print(id(l1))
  7.  
    print(id(l2))
  8.  
    a[ 0] = 11
  9.  
     
  10.  
    print(id(l1))
  11.  
    print(id(l2))
  12.  
    print(l1)
  13.  
    print(l2)
  14.  
    Output:
  15.  
    [ 3, 4, [1, 2]]
  16.  
    [ 3, 4, [1, 2]]
  17.  
    140624327425704
  18.  
    140624326197400
  19.  
    140624327425704
  20.  
    140624326197400
  21.  
    [ 3, 4, [11, 2]]
  22.  
    [ 3, 4, [11, 2]]

 

 

   It can be seen shallow copy, the equivalent of only one copy, where the a, a variation of which will change the value.

    There are various ways in Python shallow copy, copy copy module functions, copy function object, the factory method, slicing; in most cases, the preparation procedures are the use of shallow copy, unless there are specific needs; shallow copy advantages: speed copying, small footprint, high copy efficiency.

   Deep copy

Different from the shallow copy copy only the top-level references, deep copy will copy layer by layer, until all copies of the references cited so far are immutable.

  1.  
    a = [ 1, 2]
  2.  
    l1 = [ 3, 4, a]
  3.  
    l2 = copy.deepcopy(l1)
  4.  
    print(l1)
  5.  
    print(l2)
  6.  
    print(id(l1))
  7.  
    print(id(l2))
  8.  
    a[ 0] = 11
  9.  
     
  10.  
    print(id(l1))
  11.  
    print(id(l2))
  12.  
    print(l1)
  13.  
    print(l2)
  14.  
     
  15.  
    Output:
  16.  
    [ 3, 4, [1, 2]]
  17.  
    [ 3, 4, [1, 2]]
  18.  
    140673014398488
  19.  
    140672779715720
  20.  
    140673014398488
  21.  
    140672779715720
  22.  
    [ 3, 4, [11, 2]]
  23.  
    [ 3, 4, [1, 2]]

 

 

 

 

references

https://www.jianshu.com/p/9ed9b5ce7bb0

https://baijiahao.baidu.com/s?id=1627356407968660842&wfr=spider&for=pc

Guess you like

Origin www.cnblogs.com/zhaoyingzhe/p/12394630.html