Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'python,': 0.02; '16,': 0.03; 'subject:Python': 0.05; 'c++,': 0.07; 'wrapper': 0.07; 'api': 0.09; 'python': 0.09; 'defined,': 0.09; 'specific.': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr :python-list': 0.10; 'url:)': 0.13; '(but': 0.15; '(unlike': 0.16; 'curious.': 0.16; 'url:html)': 0.16; 'wrappers': 0.16; 'wrote:': 0.17; 'stefan': 0.17; 'module': 0.19; 'code.': 0.20; 'python?': 0.20; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; 'idea': 0.24; 'least': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'implemented': 0.27; 'c++': 0.27; 'interface': 0.27; 'this?': 0.28; 'wrap': 0.29; 'case,': 0.29; 'this.': 0.29; 'worked': 0.30; 'e.g.': 0.30; 'code': 0.31; 'url:python': 0.32; '(2)': 0.32; 'quickly': 0.32; 'could': 0.32; "can't": 0.34; 'agree': 0.34; 'received:google.com': 0.34; '(1)': 0.34; 'doing': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'there': 0.35; 'but': 0.36; 'url:org': 0.36; 'october': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'things': 0.38; 'url:docs': 0.38; 'performance': 0.39; 'easily': 0.39; 'where': 0.40; 'end': 0.40; 'your': 0.60; 'url:index': 0.61; 'between': 0.63; 'email addr:gmail.com': 0.63; 'differences': 0.65; 'yourself': 0.77; 'mind:': 0.84 Newsgroups: comp.lang.python Date: Tue, 16 Oct 2012 04:46:17 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=137.17.109.98; posting-account=QNsEXgoAAABu_Avbf026MEK8WO7y27Mt References: <0423426c-c19a-4634-9f67-5bf92d766f61@googlegroups.com> User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 137.17.109.98 MIME-Version: 1.0 Subject: Re: Providing a Python wrapper to a C++ type. From: Marco Nawijn To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: Lines: 77 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1350387982 news.xs4all.nl 6970 [2001:888:2000:d::a6]:56815 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:31390 On Tuesday, October 16, 2012 1:39:44 PM UTC+2, Stefan Behnel wrote: > Marco Nawijn, 16.10.2012 12:17: >=20 > > On Tuesday, October 16, 2012 10:11:52 AM UTC+2, aaron.l...@gmail.com wr= ote: >=20 > >> I have a C++ module where I have a defined, working type. How would I >=20 > >> make a wrapper for this type to be able to be used in Python? I am >=20 > >> familiar(-ish) with the C-API for functions but I can't see concretely= how >=20 > >> one would include an interface to a type. >=20 > >> >=20 > >> Is it this? http://docs.python.org/release/2.7.3/extending/newtypes.ht= ml >=20 > >=20 >=20 > > There are a few ways of doing this. At least three come to my mind: >=20 > > 1. Wrap the C++ type yourself by using handcrafted code implemented wit= h the Python C API >=20 > > 2. Use SWIG to wrap the C++ code and (semi) automatically create the wr= apper (http://www.swig.org/) >=20 > > 3. Use BOOST Python to wrap the C++ code (http://www.boost.org/doc/libs= /1_51_0/libs/python/doc/index.html) >=20 > >=20 >=20 > > I would highly discourage (1) unless you are very brave and curious. Of= course it is a nice excercise, but if you want something to work quickly I = would recommend to use either (2) or (3). >=20 > >=20 >=20 > > 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 intere= sted in the Python wrapping (SWIG could generate many other wrappers as wel= l). >=20 >=20 >=20 > There's also Cython, which provides a very flexible way (unlike SWIG) of >=20 > doing these things easily (unlike C++ with Boost). >=20 >=20 >=20 > http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html >=20 >=20 >=20 > I agree with discouraging 1) in specific. >=20 >=20 >=20 > Stefan Hi Stefan, I never worked with Cython (but I know it is very powerful and interesting)= but in my mind there are slight differences in usage scenario between e.g.= Boost Python and Cython. For me the idea of Cython is that your main code = is in Python, but you want to improve the performance of specific parts of = the code. In that case, Cython is the way to go. In case of Boost Python, t= he scenario for me is that you have a main program/library in C++, but you = want to be able use the functionality from Python.=20 Do you agree with this view? Marco