anything like C++ references?

Stephen Horne intentionally at blank.co.uk
Sun Jul 13 09:56:18 EDT 2003


On Sun, 13 Jul 2003 05:50:46 -0600, Dave Brueck
<dave at pythonapocrypha.com> wrote:

>On Sunday 13 July 2003 01:41 am, Tom Plunket wrote:

>> In C++ it's trivial to return multiple values
>
>Trivial but non-intuitive until you convince yourself "that's the way it is", 
>perhaps. :) What I mean is this: when you're just starting out you learn that 
>functions can return values like this:
>
>int foo(int a, int b)
>{
>  return a + b;
>}
>
>But the method for returning more than one thing is not a simple progression 
>from this pattern. Instead you learn and shift to a *completely* different 
>mechanism. In Python, however, you *can* continue along the original route as 
>well:
>
>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.

>> > IOW, if you could erase the influence of previous languages would
>> > this FAQ become "how can I return multiple things from a
>> > function" more often than it would become "how can I modify an
>> > object from inside a function"?

There's more to it than this.

Take the Pascal family of languages. These have an explicit
distinction between functions and procedures which is hidden in C and
Python.

In my view, the distinction between a procedure (an imperative tool
which may modify parameters in place) and a function (which returns a
value but - normally, at least - leaves its parameters unmodified) is
a useful one. Writing code to respect these conventions, even when the
language doesn't explicitly support it, has considerable benefits -
evaluation order in expressions becomes much less of an issue, for
instance, effectively eliminating at least one source of subtle
hard-to-trace errors.

Which suggests a new 'procedure' syntax for Python, either
automatically treating all parameters as references or providing a
syntax equivalent to the Pascal 'var'.





More information about the Python-list mailing list