__getitem__

Giorgi Lekishvili gleki at gol.ge
Fri Jan 25 02:51:01 CET 2002


Hi!

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.

Then, I made a SWIG interface file:
#####
%module matrix
%{
#include "matrix.h"
%}

%include "matrix.h"


extern matrix* m_new(int nrows, int ncols);
extern int m_init(matrix* m);
extern int m_add(matrix* m_sum, matrix* m1, matrix* m2);

extern int m_addrow(matrix* m,char* words[], int at);
extern void m_free(matrix* m);
extern float m_item(matrix *m, int i, int j);
extern float* m_item_ptr(matrix* m, int i, int j);

extern int m_getrow(matrix* row, matrix* m, int which_row);


%addmethods matrix {
 matrix(int nrows, int ncols){
  matrix* m;
  if (m=m_new(nrows,ncols)==NULL)
   return m;
  m_init(m);
  return m;
  };
 ~matrix(){
  m_free(self);
  };
 float* __getitem__(int i, int j){
  return m_item_ptr(self, i, j);
  };
 };
##########

the matrix structure is as follows:
typedef struct MATRIX {
  int ncols, nrows;
  float* data;
} matrix;
###########

import matrix

class PyM:
 def __init__(self, i, j):
  self.this=matrix.new_matrix(i,j)
  self.nrows=i
  self.ncols=j

 def __getitem__(self, i, f):
  return matrix.matrix___getitem__(self.this,i,j)
#########

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