Newbie question about string(passing by ref)

Adam Atlas adam at atlas.st
Thu May 10 17:57:34 EDT 2007


On May 10, 5:47 pm, Adam Atlas <a... at atlas.st> wrote:
> On May 10, 5:43 pm, lazy <arunm... at gmail.com> wrote:
>
> > I want to pass a string by reference.
>
> Don't worry, all function parameters in Python are passed by reference.

Actually, just to clarify a little bit if you're understanding "pass
by reference" in the sense used in PHP, sort of C, etc.: In Python,
you have objects and names. When I say all function parameters are
passed by reference, I don't mean you're actually passing a reference
to the *variable*. (Like in PHP where you pass a variable like &$foo
and the function can change that variable's value in the caller's
scope.) You can never pass a reference to a variable in Python. But on
the other hand, passing a parameter never causes the value to be
copied. Basically, you have a variable that's a reference to an object
somewhere in memory, and passing it to a function gives that
function's scope a new pointer to that same location in memory. So if
the function you pass it to assigns some other value to the variable,
that's all it's doing: reassigning a local name to point to somewhere
else in memory.




More information about the Python-list mailing list