SWIG interface to access C array members

jsaul jsaul at gmx.de
Wed Nov 20 10:05:01 EST 2002


Hi there,

I've got stuck with the following problem. In a C++ extension
module, which I create from existing C++ code using SWIG, I need
to access elements of array members of a class. Here is a
trimmed-down version:

%module xyz
%{
#include "xyz.h"
%}

class xyz {
    public:
        xyz();

        int a[10];
};

In Python, this doesn't quite behave as I would want it to:

>>> import xyz
>>> xx=xyz.xyz()
>>> xx.a
'_e0011708_p_int'
>>> xx.a[0]
'_'

That is I get Python-'a' as textual representation of address and
type of C-'a'. Through extending my class:

%extend xyz {
  int __getitem__(int i) {
    return self->a[i];
  };
}

I get a little bit closer to what I want:

>>> xx[0]
1

which is what I initialized 'a' with. But still, I would like to
be able to write

>>> xx.a[0]

Any ideas about what I have to do? I don't want to touch the
original codes.

Cheers, jsaul
-- 
Que le gusta rrrodarrr la errre.



More information about the Python-list mailing list