Russel Winder, 13.03.2014 17:29:
> On Thu, 2014-03-13 at 16:57 +0100, Stefan Behnel wrote:
> […]
>> The thing is: if you have to write your own wrapper anyway (trivial or
>> not), then why not write it in Cython right away and avoid the intermediate
>> plain C level?
> 
> If the task is two write an adapter (aka wrapper) then perhaps use SWIG
> whcih is easier for this task than writing Cython code.

Depends. SWIG is nice if you have a large API that a) you want to wrap
quickly all at once and b) that matches the tool well. Once you're beyond
the "matches the tool well" spot, however, you'll start having an
increasingly hard time pushing the tool into matching your API.

Cython has a higher learning curve to get started (it's a programming
language, not a wrapper generator by itself, use something like XDress for
that), but is unlimited in what it allows you to do (because it's a
programming language). So things won't suddenly become harder (let alone
impossible) afterwards.


>> It's usually much nicer to work with object oriented code on both sides
>> (assuming you understand the languages on both sides), than to try to
>> squeeze them through a C-ish API bottleneck in the middle.
> 
> It could be that "object oriented" is a red herring. Without details (*)
> of what it is about the C++ code that is the connection between Python
> and C++, it is difficult to generalize.

Sure. I've seen both good and bad API designs in C++, as in any other language.


> ctypes can be a real pain when trying to call C++ from Python using
> argument values that are not primitive types. CFFI solves (currently
> much, soon most) of this problem by addressing the adapter between
> Python and C++ in a different way to that employed by ctypes. In both
> cases, both are a lot easier than writing Cython code. 

Now, that's a bit overly generalising, wouldn't you say? Even in the cases
where cffi is as simple as Cython, I'd still prefer the portability and
simplicity advantage of having statically compiled (and tested) wrapper
code over a mix of a hand written C++-to-C wrapper and some dynamically
generated glue code with its own set of runtime dependencies. But I can
certainly accept that tools like ctypes and cffi have their niche, too. If
you're comfortable with them, and they fit your needs, then sure, use them.
There isn't one tool that caters for everyone.

Stefan


_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to