Can Python function return multiple data?
BartC
bc at freeuk.com
Fri Jun 5 05:51:01 EDT 2015
On 04/06/2015 15:37, Grant Edwards wrote:
> On 2015-06-04, Marko Rauhamaa <marko at pacujo.net> wrote:
>> Steven D'Aprano <steve at pearwood.info>:
>>
>>> But you still find a few people here and there who have been exposed
>>> to Java foolishness, and will argue that Python is "pass by value,
>>> where the value is an implementation dependent reference to the thing
>>> that you thought was the value".
>>
>> Why fight terminology? Definitions can't be proved right or wrong.
>>
>> Anyway, I would say Python definitely is in the classic pass-by-value
>> camp. Here's a simple test:
>>
>> def f(x):
>> x = 3
>>
>> y = 1
>> f(y)
>> print(y)
>>
>> If it prints 1, it's pass by value. If it prints 3, it's pass by
>> reference.
>
> Somebody else might just as honestly say that it's pass by reference:
>
> def f(x):
> x[2] = 2;
>
> x = ['a','b','c']
> f(x)
> print(x)
>
> If it prints ['a','b','c'], it's pass by value. If it's pass by
> reference, it prints ['a', 'b', 2].
>
> IMO, it's pass by reference.
No, it's not. Pass-by-reference specifically means (as Steven D'Aprano
pointed out some weeks back in another thread) that you can directly
change the caller's 'variable' so that it becomes something else, such
as a string.
In your example, the caller's x is still a list. And the same list
(assign it to y before the call. y will be ['a','b','2'] too, and (x is
y) is True).
To modify, mutate or in any way update the data associated with the
caller's argument, pass-by-reference is not needed. But it's not exactly
pass-by-value either.
In a language like Python, such a limited choice would be too naive,
because pointers and references are also used internally and
transparently, and they help to confuse matters.
If you want to call it something, perhaps call it call-by-handle.
Because a handle to the data is being passed. In the same way that if X
is a handle to a file, a window, image, or any external resource, then:
func(X)
might update the file, draw into the window or image or whatever, but
no-one would be talking about whether that file or window is passed by
value or by reference!
--
Bartc
More information about the Python-list
mailing list