python函数传参是传值还是传引用

2025-04-16 09:33:31
推荐回答(1个)
回答1:

那要看数据类型了,int,float,str这种就是传值,list,dict,类的实例,自定义对象都是穿引用。

下面是示例代码:

def change(int1,float1,str1,dict1,obj1,list1):
    int1+=1
    float1+=1
    str1+='changed'
    dict1['none_exist_key']='none_exist_value'
    obj1=None
    list1.append('change')
class obj:
    pass
int1=0
float1=0.0
str1='origin'
dict1={'key':'value'}
obj1=obj()
list1=['only_element']
print(int1)
print(float1)
print(str1)
print(dict1)
print(obj1)
print(list1)
change(int1,float1,str1,dict1,obj1,list1)
print('after change')
print(int1)
print(float1)
print(str1)
print(dict1)
print(obj1)
print(list1)