anything like C++ references?

Dave Brueck dave at pythonapocrypha.com
Sat Jul 12 22:15:36 EDT 2003


On Saturday 12 July 2003 04:56 pm, Ian Bicking wrote:
> On Sat, 2003-07-12 at 17:45, Brian Quinlan wrote:
> > > void change(int& i)
> > > {
> > >    i++;
> > > }
> >
> > The idiomatic way to write this code in python would be:
> >
> > def change(val):
> > 	return val + 1
>
> To be more specific, you would achieve the same effect with:
>
> def change(val):
>     return val + 1
> i = change(i)
>
> As opposed to the C++ where you'd do:
>
> change(i) // no assignment needed
>
>
> There is no direct Python equivalent to the C++ function, for all sorts
> of reasons (most of them very deliberate).

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. 

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"?

-Dave





More information about the Python-list mailing list