Pass variable by reference

Chris Angelico rosuav at gmail.com
Tue May 6 06:45:20 EDT 2014


On Tue, May 6, 2014 at 8:38 PM, Marko Rauhamaa <marko at pacujo.net> wrote:
> 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.

Oh, absolutely. As in many other cases, you shouldn't ask "How do I do
<some non-Python idiom> in Python", but should ask "How do I
accomplish <some goal> in Python". Only in a few cases is the first
question reasonable (eg "How do I call this function from Python",
naming some function from a C library), and even then, it's
semantically arguable.

ChrisA



More information about the Python-list mailing list