Parameter passing return value shallow copy deep copy in Python

  • 2021-07-01 07:54:19
  • OfStack

1. Parameter passing for Python

Parameter passing of Python, there is no control over reference passing or value passing. For parameters of immutable objects (numbers, characters, tuples, etc.), it is more similar to value passing; For mutable objects (lists, dictionaries, etc.), it is more similar to reference passing.


def fun1(n):
  print(n)  # n Before modification, the address pointed to and the main In a function n Points to the same address 
  n = 20   # n After the modification, the pointing address changes, which is equivalent to creating a new 1 The value is 20 Parameters of n
def fun2(l):
  print(l)  # l Before modification, the address pointed to and the main In a function l Points to the same address 
  l = [5,6,7,8,9] # l After modification, the address pointed to and the main In a function l The address pointed to is still the same. At this time, main In a function l The value of will also change 
if __name__=="__main__":
  n = 10
  l = [1,2,3,4,5]
  fun1(n)
  fun2(l)
  print(n)  # n Or the value of 10
  print(l)  # l The value of is in the fun2() Has been changed 

2. Return value of Python

The return value of Python, there is no control over reference passing or value passing. For parameters of immutable objects (numbers, characters, tuples, etc.), it is more similar to value passing; For mutable objects (lists, dictionaries, etc.), it is more similar to reference passing.


str1 = 'hi'
l1 = [1,2,3,4,5]
def fun1():
  return str1
def fun2():
  return l1
str2 = fun1() #  At this point, str1  And  str2  The pointing address of is the same 
str2 = 'hello' #  After modification, str2  The address pointed to has changed, which is equivalent to creating a new 1 The value is `hello` Variables of 
l2 = fun2() #  At this point, l1  And  l2  The pointing address of is the same 
l2 = [6,7,8,9] #  After modification, l2  The address pointed to is still the same, modify  l2  Will affect  l1

3. Shallow and deep copies of Python

For immutable objects (numbers, characters, tuples, etc.), direct assignment results and deep copy 1; For variable objects (lists, dictionaries, etc.), direct assignment, shallow copy and deep copy have different results.


#  Examples of immutable objects (numbers, characters, tuples, etc.) 
a = 100 
b = a
b = 30
print('a: ', a)
print('b: ', b)
#  Results: 
# a = 100
# b = 30
#  Examples of mutable objects (lists, dictionaries, etc.) 
import copy
dict1 = {1:1, 'user':'test', 'num':[1, 2, 3]}
dict2 = dict1 #  Direct assignment, two dictionaries pointing to addresses 1 To 
dict3 = dict1.copy() #  Shallow copy, copy only to the original object 
dict4 = copy.deepcopy(dict1) #  Deep copy, in addition to copying the original object, also copies the child objects 
dict1[1] = 11
dict1['user'] = '123'
dict1['num'].remove(1)
print(' After the original dictionary is revised, :',dict1)
print(' Direct assignment :',dict2)
print(' Shallow copy :',dict3)
print(' Deep copy :',dict4)
#  Results 
#  After the original dictionary is revised, :{1:11,'user': '123', 'num': [2, 3]}
#  Direct assignment :{1:11,'user': '123', 'num': [2, 3]}
#  Shallow copy: {1:1,'user': 'test', 'num': [2, 3]}
#  Deep copy: {1:1,'user': 'test', 'num': [1, 2, 3]}

Summarize


Related articles: