Submitted by Mr. Gung (not verified) on Fri, 13/01/2006 - 17:12.
Arguments in python are passed by value, BUT that value is a reference. If it would be passed by reference, given the following code
>> def func(arg):
... arg = ['another value']
...
>>> value = ['original value']
>>> func(value)
A
>> print value
should display - guess - ['another value'].
It gives ['original value'], though.
So, there is no pass-by-reference in Python. Never.
Try equivalent code in C++ and the reference-operator, then you'll see that pass-by-reference indeed yields ['another value'].
Using some function on the object-argument (which changes the object's state like append does) does not show whether the mechanism used is -by-reference or -by-value.
pass-by-reference and pass-by-value
Arguments in python are passed by value, BUT that value is a reference. If it would be passed by reference, given the following code
>> def func(arg):
... arg = ['another value']
...
>>> value = ['original value']
>>> func(value)
A
>> print value
should display - guess -
['another value'].It gives
['original value'], though.So, there is no pass-by-reference in Python. Never.
Try equivalent code in C++ and the reference-operator, then you'll see that pass-by-reference indeed yields
['another value'].Using some function on the object-argument (which changes the object's state like
appenddoes) does not show whether the mechanism used is -by-reference or -by-value.