user2253054
user2253054

Reputation: 95

How do I assign a value to a multidimensional array element in a function with index and value as parameters

I am trying to do something simple (i think). However, I obviously don't understand something about what is going on.

This should be a straight forward process. I am obviously not understanding something rather basic here. An explanation of what is going on and what to do would be greatly appreciated. Thanks.

Upvotes: 0

Views: 189

Answers (1)

Sven Marnach
Sven Marnach

Reputation: 601539

When accessing an item of a multi-dimensional array with the syntax a[i, j, k], the argument inside the square bracket operator is actually a tuple, not a list. It is equivalent to writing a[(i, j, k)], but inside the square brackets you are allowed to omit the parentheses. When calling a regular function, they are mandatory though.

s.set((1, 1), [2, 2])
print s.get((1, 1))

I'd suggest overriding __getitem__() and __setitem__() instead, so you can use the regular square bracket operator for your custom class.

Upvotes: 2

Related Questions