Newbie question about string(passing by ref)

lazy arunmail at gmail.com
Thu May 10 18:19:53 EDT 2007


Thanks all.

> 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.

So, just to make sure even if I return a value, there is no copy done.
Is it correct?
For eg:

def blah:
   long_str="...."
   return long_str

my_str=blah() <=== So here there is no copy done but, my_str points to
the same memory where long_str was created.

Thanks again.

On May 10, 2:57 pm, Adam Atlas <a... at atlas.st> wrote:
> 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