Suppose I have a class:
class Data:
data: np.ndarray
def __init__(self):
self.data = np.zeros((10,10))
self.str2col = { str(i):i for i in range(0,10) }
def __get_col(self,i):
"""
@param i: STR or INT
"""
if isinstance(i,int):
return i
if isinstance(i,str):
return self.str2col[i]
raise Exception("Invalid index")
Assuming I haven't made any errors, I can index this class as follows:
i = '4'
j = 4
obj = Data()
point = obj.data[obj.__get_col(i),obj.__get_col(j)]
The question is: how does one, in python, overload the "[]" such that I can write, in full numpy indexing syntax,
data = obj[:,:]
data = obj['1',:]
data = obj[:,'2']