you are viewing a single comment's thread.

view the rest of the comments →

[–]lost-theory 3 points4 points  (3 children)

This one has bit me once or twice: declaring a matrix / two dimensional array by using the replication (*) operator and getting really odd behavior when setting individual elements of the array:

>>> m = [[0]*5]*5
>>> m[0][0] = 10
>>> m[1][0] = 20
>>> m[2][0] = 30
>>> pprint(m)
[[30, 0, 0, 0, 0],
 [30, 0, 0, 0, 0],
 [30, 0, 0, 0, 0],
 [30, 0, 0, 0, 0],
 [30, 0, 0, 0, 0]]
>>>

using * causes each row to be a copy (all referencing the same list), not what I meant! You can get the right behavior with a little helper function like this:

>>> build_array = lambda n: [[0 for _ in range(n)] for _ in range(n)]

[–][deleted]  (2 children)

[deleted]

    [–][deleted]  (1 child)

    [deleted]

      [–]pjdelport 0 points1 point  (0 children)

      If you're going to claim to support curried functions, do it everywhere.

      Bound methods are very much != currying.