anything like C++ references?

Dave Brueck dave at pythonapocrypha.com
Sun Jul 13 07:50:46 EDT 2003


On Sunday 13 July 2003 01:41 am, Tom Plunket wrote:
> Dave Brueck wrote:
> > Your response above (that the normal thing to do in Python is
> > just return the modified value) made me wonder if most people are
> > asking about pass-by-reference not because they want pass-by-
> > reference, but because in C it's generally a nuisance to return
> > stuff from functions, especially multiple values, so you end up
> > learning about pointers and/or pass-by-reference.
>
> 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

> > 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"?
>
> That's a good idea, although I would have to say that multiple
> return values typically means that your function is doing too
> many things.  ;)

Far from it:

lastName, firstName = line.split('\t')

(there are many, many, many such examples). This is also an example of what I 
mean by "in C it's genreally a nuisance to return multiple values":

void split(char *pLine, char **ppLastName, char **ppFirstName) {}

Sadly, it's not uncommon to see one-off structures defined whose sole purpose 
in life is to act as a go-between for functions. Ugh.

-Dave





More information about the Python-list mailing list