tags:

views:

1143

answers:

5

I have implemented a class in C++. I want to use it with Python. Please suggest step by step method and elaborate each step. Somthing like this...

class Test{
     private:
        int n;
     public:
        Test(int k){
            n=k;
        }
        void setInt(int k){
            n = k; 
        }
        int getInt(){
            return n;
        }
};

Now, in Python

>>> T1 = Test(12)
>>> T1.getInt()
12
>>> T1.setInt(32)
>>> T1.getInt()
32

Please suggest.How can I do this ? NOTE: I would like to know manual way to do that. I don't want any third party library dependency.

+4  A: 

I would suggest you try SWIG or sip (KDE/PyQt).

SWIG link : http://www.swig.org/
SIP link: http://freshmeat.net/projects/python-sip/

These can be used to wrap C++ classes and provide a Pythonic interface to them.

batbrat
+5  A: 

Look into Boost.Python. It's a library to write python modules with C++.

Also look into SWIG which can also handle modules for other scripting languages. I've used it in the past to write modules for my class and use them within python. Works great.

You can do it manually by using the Python/C API, writing the interface yourself. It's pretty lowlevel, but you will gain a lot of additional knowledge of how Python works behind the scene (And you will need it when you use SWIG anyway).

Johannes Schaub - litb
Doesn't meet "I don't want any third party library dependency" requirement.
Constantin
You will need the python library anyway. it includes the python/c api of course.
Johannes Schaub - litb
+7  A: 

Here is a pretty long article explaining different ways to do it.

Andrew Hare
+3  A: 

ctypes is good. It is really easy to use, and it comes standard with Python. Unfortunately it can only talk to shared libraries or DLLs, which means you couldn't directly interface to a C++ object. But you could use a handle system.

>>> getInt(h)
12

I think that is simple, easy to understand, and doesn't require extra libraries.

dangph
A: 

Can we use the above said methods to make extensions in Symbian C++ for PyS60.

Xolve
That looks more like a question than an answer.
mizipzor