Understanding python functions - Instant Python tutorial

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri Jul 13 04:55:45 CEST 2007


En Thu, 12 Jul 2007 21:51:08 -0300, Chris Carlen  
<crcarleRemoveThis at BOGUSsandia.gov> escribió:

> Hi:
>
> I have begun learning Python by experimenting with the code snippets  
> here:
>
> http://hetland.org/writing/instant-python.html
>
> In the section on functions, Magnus Lie Hetland writes:
>
> --------------------------------------------------------------------
> For those of you who understand it: When you pass a parameter to a
> function, you bind the parameter to the value, thus creating a new
> reference. If you change the “contents” of this parameter name (i.e.
> rebind it) that won’t affect the original. This works just like in Java,
> for instance. Let’s take a look at an example:
>
> def change(some_list):
>      some_list[1] = 4
>
> x = [1,2,3]
> change(x)
> print x # Prints out [1,4,3]
>
> As you can see, it is the original list that is passed in, and if the
> function changes it, these changes carry over to the place where the
> function was called.  Note, however the behaviour in the following  
> example:
>
> def nochange(x):
>      x = 0
>
> y = 1
> nochange(y)
> print y # Prints out 1
>
> Why is there no change now? Because we don’t change the value! The value
> that is passed in is the number 1 — we can’t change a number in the same
> way that we change a list. The number 1 is (and will always be) the
> number 1. What we did do is change the contents of the local variable
> (parameter) x, and this does not carry over to the environment.
> --------------------------------------------------------------------
>
> What this looks like to me is what would happen if in C I passed a
> pointer to the list x to the function change(), as in:
>
> change(&x);
>
> Thus the function could change the original list.
>
> 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?

First, see this short article http://effbot.org/zone/python-objects.htm

Now, forget about C "variables" and "pointers" because you won't get much  
far with those concepts.
Objects exist - and we usually use names to refer to them. This line:

a =  1

means "make the name 'a' refer to the object 1", or, "bind the name 'a' to  
the instance of type int with value 1", or "let 'a' be a reference to the  
object 1"

This line:

some_list[1] = 4

means "make the second element of some_list refer to the object 4", or  
"alter some_list so its element [1] is a reference to the object 4"

bind the name 'a' to the instance of type int with value 1", or "let 'a'  
be a reference to the object 1"

Note that some objects are immutable - like the number 1, which will  
always be the number 1 (*not* an integer "variable" that can hold any  
integer value). Other objects -like lists and dictionaries, by example, or  
most user defined classes- are mutable, and you can change its contents  
and properties. Modifying an object is not the same as rebinding its name:

x = [1,2,3]
y = x
x[1] = 4
print x		# [1, 4, 3]
print y 	# [1, 4, 3]

x = [1,2,3]
y = x
x = [1,4,3]
print x		# [1, 4, 3]
print y		# [1, 2, 3]

You can test is two names refer to the same object using the is operator:  
x is y. You will get True in the first case and False in the second case.

-- 
Gabriel Genellina




More information about the Python-list mailing list