Question about Pass-by-object-reference?

Peter Pearson ppearson at nowhere.invalid
Tue Jul 22 22:35:33 CEST 2014


On Tue, 22 Jul 2014 12:34:51 -0700 (PDT), fl <rxjwg98 at gmail.com> wrote:
[snip]
>
> But I don't understand the reassign function result:
>
>>>> def reassign(list):
> ... 	list=[0,1]
> ... 
>>>> list=[0]
>>>> reassign(list)
>>>> print list
> [0]

When you say "def reassign(list)", that means "I'm defining a function
to which the caller will pass one object, and within this function I'm
going to refer to that object by the name 'list'."

Then, when you say "list=[0,1]", that means "Create the object [0,1],
and assign to it the name 'list'."  At this point, there is no longer
any name that refers to the object that the caller passed.

You might have thought that "list=[0,1]" would modify the caller-passed
object, but that's not what happens.  That's not what "=" means.

-- 
To email me, substitute nowhere->spamcop, invalid->net.



More information about the Python-list mailing list