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


Groups > comp.lang.python > #31387

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

From Stefan Behnel <stefan_ml@behnel.de>
Subject Re: Providing a Python wrapper to a C++ type.
Date 2012-10-16 13:39 +0200
References <b71bb1ac-cfe0-45eb-a384-4d6109c99543@googlegroups.com> <0423426c-c19a-4634-9f67-5bf92d766f61@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.2263.1350387583.27098.python-list@python.org> (permalink)

Show all headers | View raw


Marco Nawijn, 16.10.2012 12:17:
> On Tuesday, October 16, 2012 10:11:52 AM UTC+2, aaron.l...@gmail.com wrote:
>> 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
> 
> 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).

There's also Cython, which provides a very flexible way (unlike SWIG) of
doing these things easily (unlike C++ with Boost).

http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html

I agree with discouraging 1) in specific.

Stefan

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