__getitem__
Tim Hochberg
tim.hochberg at ieee.org
Thu Jan 24 18:25:36 CET 2002
Hi Girogi,
"Giorgi Lekishvili" <gleki at gol.ge> wrote ...
> I must have been doing a stupid error. The situation is like follows:
>
> First, I have written a simple matrix module in C. This works fine.
[SNIP]
> float* __getitem__(int i, int j){
> return m_item_ptr(self, i, j);
> };
[SNIP]
> class PyM:
[SNIP]
> def __getitem__(self, i, f):
> return matrix.matrix___getitem__(self.this,i,j)
> #########
These are likely your problem: __getitem__ doesn't work how you think it
does! m[0][0] gets translated into something like:
m.__getitem__[0].__getitem__[0]
This only supplies two arguments (m and 0) to each of the getitem calls.
Hence your error.
I'm rusty with Swig, and there's more than one way you could do it, but I'd
suggest something like:
def __getitem__(self, index):
i, j = index
return matrix.matrix___getitem__(self.this, i, j)
Then you would call it as:
>>> m[i,j]
This gets translated to:
m.__getitem__((i,j))
The arguments get packed up in a tuple which you can then unpack.
(Or you could just use Numeric...)
-tim
> however,
> >>>import PyM
> >>>m=PyM.PyM(3,4)
> >>>m[0][0]
> TypeError: not enough argumets, expected 3, got 2.
>
> #############
>
> What's wrong?
>
> Thanx in advance.
> GRTZ,
> Giorgi
>
>
More information about the Python-list
mailing list