Parameter passing in python function

In learning Python, I have doubts about parameter passing in Python. I want to know whether it is a copy or a reference address, so I have a test.
Personal understanding, please point out any errors.

test

print("==========列表==========")
def listChanged(a): # 列表:同一引用
    a[0] = 2
    print(id(a))

a = [1]
print("调用函数前:" + str(a))
listChanged(a)
print(id(a))
print("调用函数后:" + str(a))

print("==========集合==========")
def setChanged(a): # 集合:同一引用
    a.add(2)
    print(id(a))

a = {
    
    1}
print("调用函数前:" + str(a))
setChanged(a)
print(id(a))
print("调用函数后:" + str(a))

print("==========字典==========")
def dictChanged(a): # 字典:同一引用
    a[1] = "2"
    print(id(a))

a = {
    
    1:"1"}
print("调用函数前:" + str(a))
dictChanged(a)
print(id(a))
print("调用函数后:" + str(a))

print("==========数值==========")
def numberChanged(a): # 数值:结果变成不同引用
    print("调用函数时:", id(a))
    a += 1
    print("重新赋值:", id(a))

a = 1
print("调用函数前:" + str(a))
numberChanged(a)
print(id(a))
print("调用函数后:" + str(a))

print("==========字符串==========")
def stringChanged(a): # 字符串:结果变成不同引用
    print("调用函数时:", id(a))
    a += "a[:]" # 假装拷贝
    print("重新赋值:", id(a))

a = "1"
print("调用函数前:" + str(a))
stringChanged(a)
print(id(a))
print("调用函数后:" + str(a))

print("==========元组==========")
def tupleChanged(a): # 元组:同一引用
    print("调用函数时:", id(a))
    a += ("合并两元组",)
    print("重新赋值:", id(a))

a = ("1", "2")
print("调用函数前:" + str(a))
tupleChanged(a)
print(id(a))
print("调用函数后:" + str(a))

print("==========带可变子元素的元组==========")
def tuple2Changed(a): # 带可变子元素的元组:同一引用
    a[1][0] = "22"
    print(id(a))

a = ("1", ["2"])
print("调用函数前:" + str(a))
tuple2Changed(a)
print(id(a))
print("调用函数后:" + str(a))

get:

==========列表==========
调用函数前:[1]
2268560182080
2268560182080
调用函数后:[2]
==========集合==========  
调用函数前:{
    
    1}
2268560447296
2268560447296
调用函数后:{
    
    1, 2}        
==========字典==========  
调用函数前:{
    
    1: '1'}      
2268560123776
2268560123776
调用函数后:{
    
    1: '2'}      
==========数值==========  
调用函数前:1
调用函数时: 2268558420272
重新赋值: 2268558420304
2268558420272
调用函数后:1
==========字符串==========
调用函数前:1
调用函数时: 2268560076912
重新赋值: 2268560559408
2268560076912
调用函数后:1
==========元组==========
调用函数前:('1', '2')
调用函数时: 2268560166464
重新赋值: 2268560558592
2268560166464
调用函数后:('1', '2')
==========带可变子元素的元组==========
调用函数前:('1', ['2'])
2268560559232
2268560559232
调用函数后:('1', ['22'])

to sum up

  • When passed in as a parameter, it is the passed memory address.
  • But because the immutable type cannot be changed, if you modify it, you will create a new object and open up a new space in the memory, and the modification in the function does not affect the original external variables .
  • For the transfer of variable types, because no object is created, the same object is always operated inside and outside the function.
  • The tuple of the immutable type contains [sub-element of the variable type], and its modification of the variable element still follows the logic of the variable type. That is to modify its mutable sub-object, it will not be re-created, then this tuple has not changed (referring to its memory address and stored element memory address has not changed, it is still the unchanged tuple object, but the content will be modified).
  • Immutable data : Number (number), String (string), Tuple (tuple);
  • Variable data : List (list), Dictionary (dictionary), Set (collection).

Guess you like

Origin blog.csdn.net/zsq8187/article/details/109846388