Pass variable by reference

Marko Rauhamaa marko at pacujo.net
Tue May 6 06:38:14 EDT 2014


Chris Angelico <rosuav at gmail.com>:

>>     characters, words, lines = stats.read()
>
> That's not really pass-by-reference, though. What you're doing is
> output parameters, which are usually implemented in C with pointers,
> but in Python with a return tuple.

Correct, but it is worth questioning the question itself: what do you
need pass-by-reference for? A very common case is returning multiple
values. For that, Python has other idioms available.

> And, by the way, it's not purely academic. There have been times when
> I've done exactly that as a means of passing state around. It's not
> common, but it has its place.

Nobody has been disputing Steven's technique. There are many variations
to that as well. For example, instead of a list, there might be an
appropriate class/object that could host the interesting piece of
information and act as the desired reference.


Marko



More information about the Python-list mailing list