Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #31376

Re: Providing a Python wrapper to a C++ type.

Newsgroups comp.lang.python
Date 2012-10-16 03:17 -0700
References <b71bb1ac-cfe0-45eb-a384-4d6109c99543@googlegroups.com>
Message-ID <0423426c-c19a-4634-9f67-5bf92d766f61@googlegroups.com> (permalink)
Subject Re: Providing a Python wrapper to a C++ type.
From Marco Nawijn <nawijn@gmail.com>

Show all headers | View raw


On Tuesday, October 16, 2012 10:11:52 AM UTC+2, aaron.l...@gmail.com wrote:
> Hi,
> 
> 
> 
> I have a C++ module where I have a defined, working type. How would I make a wrapper for this type to be able to be used in Python? I am familiar(-ish) with the C-API for functions but I can't see concretely how one would include an interface to a type.
> 
> 
> 
> Is it this? http://docs.python.org/release/2.7.3/extending/newtypes.html
> 
> 
> 
> Regards,
> 
> Aaron

Hi Aaron,

There are a few ways of doing this. At least three come to my mind:
1. Wrap the C++ type yourself by using handcrafted code implemented with the Python C API
2. Use SWIG to wrap the C++ code and (semi) automatically create the wrapper (http://www.swig.org/)
3. Use BOOST Python to wrap the C++ code (http://www.boost.org/doc/libs/1_51_0/libs/python/doc/index.html)

I would highly discourage (1) unless you are very brave and curious. Ofcourse it is a nice excercise, but if you want something to work quickly I would recommend to use either (2) or (3).

I have used both SWIG and BOOST Python and either of them worked pretty well for me. In the end I selected BOOST Python, because I was only interested in the Python wrapping (SWIG could generate many other wrappers as well).

Regards,

Marco

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Providing a Python wrapper to a C++ type. aaron.l.france@gmail.com - 2012-10-16 01:11 -0700
  Re: Providing a Python wrapper to a C++ type. Marco Nawijn <nawijn@gmail.com> - 2012-10-16 03:17 -0700
    Re: Providing a Python wrapper to a C++ type. Stefan Behnel <stefan_ml@behnel.de> - 2012-10-16 13:39 +0200
      Re: Providing a Python wrapper to a C++ type. Marco Nawijn <nawijn@gmail.com> - 2012-10-16 04:46 -0700
        Re: Providing a Python wrapper to a C++ type. Stefan Behnel <stefan_ml@behnel.de> - 2012-10-16 14:16 +0200
        Re: Re: Providing a Python wrapper to a C++ type. Evan Driscoll <driscoll@cs.wisc.edu> - 2012-10-16 11:42 -0500
          Re: Providing a Python wrapper to a C++ type. Christian Gollwitzer <auriocus@gmx.de> - 2012-10-16 21:03 +0200
      Re: Providing a Python wrapper to a C++ type. Marco Nawijn <nawijn@gmail.com> - 2012-10-16 04:46 -0700

csiph-web