anything like C++ references?

Hannu Kankaanpää hanzspam at yahoo.com.au
Mon Jul 14 19:30:56 CEST 2003


Stephen Horne <intentionally at blank.co.uk> wrote in message news:<5tn2hvgjqlqg1qs7omvugpvm62cuso261k at 4ax.com>...
> >def foo(a, b, c):
> >  return a+b, b+c
> 
> You are not directly returning multiple values - you are returning a
> tuple. You can return containers in C++ too. The only difference is
> that Python makes working with tuples *much* easier - but its no more
> a 'simple progression' than in C++. You still need to know about
> tuples as well as returning values.

You don't need to know about tuples so that you can understand
that multiple values are returned, and that's the difference.
I didn't know of tuples when I saw some of my first Python code,
yet the idiom for returning multiple values was trivially clear.
It became even better though when I learned it was implemented
through tuples ;).

In contrast, in C++ one needs to explicitely state make_pair
or make_tuple, requiring the user to know of the underlying
types.

Had I been required to type

def foo(a, b, c):
    return tuple(a+b, b+c)

x = foo(a, b, c)
a = x[0]
b = x[1]

Then I could've agreed with you. But multi-assignment from
tuples and implicit creation of tuples makes the returning
of multiple values look like a language feature, not a way
to abuse tuples. This ease of use is the reason it can work
as a replacement for C++'s "send-by-reference".




More information about the Python-list mailing list