Newbie: tuple list confusion.

Elaine Jackson elainejackson7355 at home.com
Mon Sep 13 00:25:47 EDT 2004


In addition to what you've found out already, there is another difference that,
to my mind, is of paramount importance; namely, the fact that an augmented
assignment (a+=b) is actually not an assignment at all. An actual assignment
(a=a+b) binds the name "a" to a new object, while a so-called augmented
assignment mutates the object a itself. I was quite confused by this point when
I first found out about it, so I'll do my best to explain it now for your
benefit. Consider the following functions:

>>> def f(a): a=a+['tackedOnItem']; return a

## The "a" on the righthand side of the assignment
## evaluates to the function's parameter, since
## at that point no local variable with that name
## has yet been declared; next the "a" on the
## lefthand side of the assignment declares a local
## variable (a function cannot rebind the name of
## one of its parameters); and finally the "a" in
## the return statement evaluates to the value of
## the local variable "a" (local bindings being
## always preferred over global ones when possible).
## Hence f returns x + ['tackedOnItem'], where x
## is its actual parameter, and that parameter is
## NOT mutated.

>>> def g(a): a+=['tackedOnItem']; return a

## Since the augmented assignment is actually a
## mutation, there is no question of rebinding the
## name of the parameter; the "a" on the lefthand
## side of the augmented assignment evaluates to
## the parameter, and the parameter gets mutated;
## there being no local variable called "a", the "a"
## in the return statement also evaluates to the
## parameter, and so g returns its mutated parameter.

>>> def h(a): a.extend(['tackedOnItem']); return a

## This function is equivalent to g, but more explicit about its side-effect.
## Here are the relevant examples:

>>> x = [1,2,3]
>>> f(x)
[1, 2, 3, 'tackedOnItem']
>>> x
[1, 2, 3]
>>> g(x)
[1, 2, 3, 'tackedOnItem']
>>> x
[1, 2, 3, 'tackedOnItem']
>>> y = [4,5,6]
>>> h(y)
[4, 5, 6, 'tackedOnItem']
>>> y
[4, 5, 6, 'tackedOnItem']

HTH


"Q852913745" <q852913745 at aol.com> wrote in message
news:20040912195529.03437.00000338 at mb-m27.aol.com...
| I am trying to learn python from 'Python programming for the absolute
beginner'
| I'm sure I understand the difference between tuples and lists, but while
| experimenting with what I have learned so far, I was suprised that this code
| worked.
|
| # Create list a
| a=["start",878,"item 1",564354,"item 2"]
| print "length of list a =",len(a)
| print "a =",a ; print
|
| # Create tuple b
| b=(234,"item 3",456,"end")
| print "length of tuple b =",len(b)
| print "b =",b ; print
|
| print "Add b onto a"  # Shouldn't work!
| #---------------------------------------------
| # a=a+b    # causes an error as it should,
| a+=b      # but why is this ok?
| #---------------------------------------------
| print "a =",a
| print "length of a =",len(a)
| print;c=raw_input("Press enter to exit")
|
| My book states that a=a+b and a+=b are the same, and that different 'types' of
| sequence cannot be joined together.





More information about the Python-list mailing list