python 函数参数传递

本文解析了Python中函数参数的传递方式,区分了可变对象与不可变对象的不同处理方法,并对比了与C语言中参数传递的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Python’s pass-by-assignment scheme isn’t quite the same as C++’s reference parameters
option, but it turns out to be very similar to the C language’s argument-passing
model in practice:
• Immutable arguments are effectively passed “by value.” Objects such as integers
and strings are passed by object reference instead of by copying, but because
you can’t change immutable objects in-place anyhow, the effect is much like making
a copy.
• Mutable arguments are effectively passed “by pointer.” Objects such as lists
and dictionaries are also passed by object reference, which is similar to the way C
passes arrays as pointers—mutable objects can be changed in-place in the function,
much like C arrays.
Of course, if you’ve never used C, Python’s argument-passing mode will seem simpler
still—it involves just the assignment of objects to names, and it works the same whether

the objects are mutable or not.

python函数参数是值传送还是引用传送只取决于传送的这个对象是可变的,还是不可变的,

如果是可变的,就是引用参数传递,否则就是值传递。


可变对象:如list、map、自定义的object;

不可变对象:如numeric、string;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值