passing by refference

Fredrik Lundh fredrik at pythonware.com
Thu May 15 17:11:53 EDT 2003


Doug Quale wrote:

> No, that's exactly what call-by-value means when applied to Python values
> (actually r-values).  What happens when you try this:
>
> >>> y = [1, 2, 3]
> >>> x = y
> >>> x[:] = [-1]*3
> >>> y
> [-1, -1, -1]
>
> Same behavior, no function calls.

except that "x[:] =" *is* a method call.

x[:] = [-1]*3 passes a slice object and the result of ([-1]*3) to
the x.__setitem__ method, using the standard calling mechanism
(that's what the "the object is asked" stuff in the language ref
means)

"x.y =" is also a method call (__setattr__).

so is "x[y] =" (__setitem__, again).

however, "x = y" isn't.

(and thirty years ago, CLU also had syntactic sugar that looked
like assignments for the uninformed observer, but really was yet
another way to call a procedure.  nothing new here.)

</F>








More information about the Python-list mailing list