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
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;