Understanding python functions - Instant Python tutorial
bignose+hates-spam at benfinney.id.au
Fri Jul 13 08:24:08 CEST 2007
Chris Carlen <crcarleRemoveThis at BOGUSsandia.gov> writes:
> 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.
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.
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.
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.
\ "If you ever teach a yodeling class, probably the hardest thing |
`\ is to keep the students from just trying to yodel right off. |
_o__) You see, we build to that." -- Jack Handey |
More information about the Python-list