Reputation: 95
I am trying to do something simple (i think). However, I obviously don't understand something about what is going on.
Here is the code.
from numpy import *
class Space():
def __init__(self, shape, mode):
self.space = ndarray(shape, dtype=list )
self.mode = mode
def get(self, elem) :
return(self.space[elem])
def set(self, elem, val):
self.space[elem] = val
shape = [2,2,2]
s = Space(shape, 'wrap')
s.set([1,1], [2,2])
print s.get([1,1])
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
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