0

In C, I can write

foobar.h:

typedef int ARR[2];

foobar.c:

#include "foobar.h"
ARR arr;

and now I have an array arr of the proper size. I can obtain that array without having to know the size, I just know the name of the type ARR. In other words, the header file hides the implementation details of ARR - the principle of encapsulation.

Now I want to translate this with SWIG to Python. I have an interface

foobar.i:

%{
#include "foobar.h"
%}

%include "foobar.h"

and that means to me, just as above, that I should be able to use the interface coming out of foobar.h without having to know the details. That is, I want to use the word ARR somehow on Python command line, to get an array of the correct size (perhaps initialized, in Python everything is initialized).

I am not trying to repeat C syntax in Python. I want to repeat the principle of encapsulation.

What do I need to add to the interface to accomplish that?

I am trying and trying and can't figure it out.

1 Answer 1

1

Encapsulate via a struct or class: you'll likely want to expose, sooner or later, the array size to help with testing on the python side, and add operations like initialize values to 0, reset(0), adding two ARR arrays, and such. Then SWIG will do everything for you.

Sign up to request clarification or add additional context in comments.

1 Comment

Agree. It's not encapsulated if can't manipulate it without knowing its size. Put it in a class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.