Understanding python functions - Instant Python tutorial
Chris Carlen
crcarleRemoveThis at BOGUSsandia.gov
Fri Jul 13 12:41:39 EDT 2007
Ben Finney wrote:
> Chris Carlen <crcarleRemoveThis at BOGUSsandia.gov> writes:
>
> def change(some_list):
> some_list[1] = 4
>
> x = [1,2,3]
> change(x)
> print x # Prints out [1,4,3]
> ---
> def nochange(x):
> x = 0
>
> y = 1
> nochange(y)
> print y # Prints out 1
>
>>I don't understand Hetland's terminology though, when he is speaking
>>of "binding" and "reference." Actually, Hetland's entire first
>>paragraph is unclear.
>>
>>Can anyone reword this in a way that is understandable?
>
> I've had some success with the following way of thinking about it.
>
> Some languages have "variables", which act like boxes that have names
> etched on the side. Once created, the box can contain an object, and
> it can be inspected while in the box; to change the variable, you
> throw out the object and put a different object in the same box.
Yes, so y = x takes a copy of the stuff in the x box and puts it in the
y box. Which is what really happens in the hardware.
> That's not how Python works. Every value is an object; the assignment
> operator binds a name to an object. This is more like writing the name
> on a sticky-note, and sticking it onto the object.
>
> * The object itself doesn't change or "move".
>
> * The object can be referred to by that name, but isn't "inside" the
> name in any way.
>
> * Assigning multiple names to the same object just means you can
> refer to that same object by all those names.
>
> * When a name goes away, the object still exists -- but it can't be
> referred to if there are no longer any names left on it.
>
> * Assigning a different object to an existing name just means that
> the same sticky-note has moved from the original object to the new
> one. Referring to the same name now references a different object,
> while the existing object keeps all the other names it had.
Excellent description. This understandable to me since I can envision
doing this with pointers. But I have no idea how Python actually
implements this. It also appears that I am being guided away from
thinking about it in terms of internal implementation.
> When you pass an object as a parameter to a function, the object
> receives a new sticky-label: the parameter name under which it was
> received into the function scope. Assignment is an act of binding a
> name to an object; no new object is created, and it still has all the
> other names it had before.
Ok, so I can understand the code above now.
In the first case I pass the reference to the list to change(). In the
function, some_list is another name referring to the actual object
[1,2,3]. Then the function changes the object referred to by the second
element of the list to be a 4 instead of a 2. (Oh, the concept applies
here too!) Out of the function, the name x refers to the list which has
been changed.
In the second case, y refers to a '1' object and when the function is
called the object 1 now gets a new reference (name) x inside the
function. But then a new object '0' is assigned to the x name. But the
y name still refers to a '1'.
I get it. But I don't like it. Yet. Not sure how this will grow on me.
> When the function ends, all the names that were created inside that
> function's scope disappear; but the objects still exist under any
> names they had previously, and if you use those names you'll be
> looking at the same object as was manipulated inside the function.
>
> When the object has lost all its names -- for example, they've
> disappeared because the scope they were in has closed, or they've been
> re-bound to other objects -- they can no longer be referenced. At some
> point after that, the automatic garbage collection will clean that
> object out of memory.
>
> This sticky-note analogy, and the behaviour described, is what is
> meant by "references". A name refers to an object; changing the object
> means that by referring to that same object under its different names,
> you will see the same, modified, object.
>
> In Python, all names are references to objects. The assignment
> operator '=' doesn't create or change a "variable"; instead, it binds
> a name as reference to an object. All functions receive their
> parameters as the existing object with a new name -- a reference to
> that object, just like any other name.
>
> Hope that helps.
A great deal of help, thanks. Excellent explanation. Wow. This is
strange. A part of me wants to run and hide under the nearest 8-bit
microcontroller. But I will continue learning Python.
--
Good day!
________________________________________
Christopher R. Carlen
Principal Laser&Electronics Technologist
Sandia National Laboratories CA USA
crcarleRemoveThis at BOGUSsandia.gov
NOTE, delete texts: "RemoveThis" and
"BOGUS" from email address to reply.
More information about the Python-list
mailing list