Re: Understanding python functions - Instant Python tutorial



Ben Finney wrote:
Chris Carlen <crcarleRemoveThis@xxxxxxxxxxxxxxx> 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@xxxxxxxxxxxxxxx
NOTE, delete texts: "RemoveThis" and
"BOGUS" from email address to reply.
.



Relevant Pages

  • Re: Capitalisation of Section, Figure, etc.
    ... a proper name (as it refers to one specific figure) and should take a ... whereas if you are just referring to figures in general it ... reference for this when I looked a while ago. ...
    (alt.usage.english)
  • Re: What am I doing wrong?
    ... Private Sub Form_Load ... You can use a reference to some object instead. ... Anytime you are assigning objects to variables, ... With Text1 added to a new form, try this code in the form: ...
    (microsoft.public.vb.general.discussion)
  • Re: why cannot assign to function call
    ... assignment/ from the values of caller's argument expressions. ... A Java reference and a C++ reference are different things, ... the fact that X refers to that object, ...
    (comp.lang.python)
  • Re: Strength or no errors?
    ... Replying to myself, just to say oops, apologies for typo. ... It refers to Witgenstein's version ... language cannot be used. ... It doesn't mean there's no possible reference! ...
    (uk.tech.digital-tv)
  • Re: Copy lines from sheet 2 to sheet 6
    ... Please keep all correspondence within the Group, ... refers to I20, that being the *first* row of the indexed range. ... SO, we just anchor the first reference by making it absolute, and only ... I would like to show the data in Sheet 2 to Sheet 6 col B row 1,2,3,4, ...
    (microsoft.public.excel.newusers)