The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

import-bot (20211) [Avatar] Offline
#1
[Originally posted by kwoeltje]

After reading section 6.5.3 in QPB I tried to initialize a 5x5 array with
zeros with the following results:

>>> alist = [[0,0,0,0,0]] * 5
>>> alist
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0,
0, 0]]
>>> alist[2][3] = 1
>>> alist
[[0, 0, 0, 1, 0], [0, 0, 0, 1, 0], [0, 0, 0, 1, 0], [0, 0, 0, 1, 0], [0, 0, 0,
1, 0]]

It turns out this is a well known pitfall and is listed in the FAQs (4.50)even
if it is non-intuitive. If you are planning on a second edition of this
excellent book, you might want to clarify this point.
>K
import-bot (20211) [Avatar] Offline
#2
Re: array intitialization
[Originally posted by daryl harms]

> After reading section 6.5.3 in QPB I tried to initialize a 5x5 array with
> zeros with the following results:
>
> >>> alist = [[0,0,0,0,0]] * 5
> >>> alist
> [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0,
> 0, 0]]
> >>> alist[2][3] = 1
> >>> alist
> [[0, 0, 0, 1, 0], [0, 0, 0, 1, 0], [0, 0, 0, 1, 0], [0, 0, 0, 1, 0], [0, 0, 0,
> 1, 0]]
>
> It turns out this is a well known pitfall and is listed in the FAQs (4.50)even
> if it is non-intuitive. If you are planning on a second edition of this
> excellent book, you might want to clarify this point.
> >K

Hi Keith,

Thanks, this is an area that I'd want to expand on. In section 6.6 there is an
introduction to it but I didn't cover it in enough detail or touch on all the
issues. If you look at the "append to nested list" thread (in the second half
of page 3 of this forum) you'll see some more discussion expanding on some of
these issues (ie. mutable object behavior) which might be worth taking a look
at.

Since bugs caused by not fully understanding this can be hard to find, this is
certainly an area in which it is worth playing around with a number of
examples in the interpreter in order to ensure you have an understanding of
what is going on.

While on the subject of mutable objects and things that should be put in any
new revision of the book, there is at least one other area where this can
catch a person.

If you pass a mutable object into a function it can be changed (i.e.):

>>> def f(a=[]):
a.append(1)
print a

>>> b = [0]
>>> f(b)
[0, 1]
>>> b
[0, 1]
>>>

This is shown in section 10.5 and is generally what people expect and doesn't
cause problems. However, what almost always causes problems is the use of the
empty list as a default value. The problem here is that a list object is
created once when the function is first loaded and this same list is
reassigned to "a" and then appended to every time "f()" is called with no
arguments:


>>> f()
[1]
>>> f()
[1, 1]
>>> f([0])
[0, 1]
>>> f()
[1, 1, 1]
>>>

This is rarely what people want to do and difficult to track down if you are
not expecting it. So, the thing to remember is that default parameters are
created once at function load time rather than each time the function is
called. The solution is to generally avoid using mutable objects as default
arguments and instead make the assignment if necessary within the body of the
function:

>>> def g(a = None):
if not a: a= []
a.append(1)
print a

>>> g()
[1]
>>> g()
[1]
>>>

Fortunately, without such things as pointers and complex method visibility
modifier rules there are fewer tricky pitfalls in Python than most other
languages. However, it unfortunately isn't totally free of them.

Sorry, for moving this reply to a related subject, but thanks for giving me an
excuse to write this down somewhere.

Best wishes,

Daryl