Saturday, March 5, 2011

How to Pass a Python Array as an Argument

Copy the array like this before you pass into the method: arrayName[:], if you don't want your array to be changed inside the method/function.

Example. Compare line 9 with line 16:

Result:
no copy:
before: [1, 1, 1, 1]
in change(): [2, 1, 1, 1]
after: [2, 1, 1, 1]

with copy:
before: [1, 1, 1, 1]
in change(): [2, 1, 1, 1]
after: [1, 1, 1, 1]

No comments:

Post a Comment