Can Python function return multiple data?

Chris Angelico rosuav at gmail.com
Thu Jun 4 19:04:51 EDT 2015


On Fri, Jun 5, 2015 at 8:52 AM, Steven D'Aprano <steve at pearwood.info> wrote:
> On Fri, 5 Jun 2015 04:38 am, Grant Edwards wrote:
>
>>>> But, discussing pass-by-this vs. pass-by-that without also discussing
>>>> the semantics of the assignment operator is rather pointless.
>>>
>>> No, that's a red-herring.
>>
>> I don't think so.  The reason that many people seem to confused about
>> Python's argument passing is that they don't understand what
>> assignment does.
>
>
> I don't see the connection, but do explain, I'm interested.

The connection is that argument passing is, in Python and in many
other languages, a form of assignment, and follows all the same rules
as assignment follows. (It's a kind of magic assignment that spans two
namespaces, but that has nothing to do with pass-by-X questions.)
These two will exhibit the same behaviour:

# Option 1
x = some_object()
y = x
... do something with y ...
... compare y and x

# Option 2
def do_stuff(y):
    ... do something with y ...

x = some_object()
do_stuff(x)
... compare y and x


Python's object semantics go far deeper than just argument passing,
but everyone's confusions about pass-by-X are always about function
calls.

Whether this makes a real difference to anything is a separate question.

ChrisA



More information about the Python-list mailing list