by michelpp on 2/19/20, 2:06 PM
Interesting article, but I wouldn't say introducing a 3rd language is the easiest way to call C code from Python, that distinction goes to the CFFI library by Armin Rigo et al. I've used it to wrap several C libraries and it's amazingly powerful. Bonus it works with CPython and PyPy.
https://cffi.readthedocs.io/en/latest/
by WalterBright on 2/19/20, 6:28 PM
> I wrote a little project called dpp because I’m lazy
I can confirm that Atila is a very lazy man. He's always figuring out ways to have his computer do the work for him. He's so successful at avoiding work, people keep giving him more work to do, making him a very hard working lazy man.
by jakogut on 2/19/20, 2:41 PM
I'm currently working on a project using Cython to wrap and expose a large and complex C API (CEF) to Python. One of the most time-consuming, tedious, and error prone tasks is translating functions and structures to Cython syntax, including managing the GIL. Also, keeping the translated API up to date with new releases.
I'm curious to see how this D approach compares, both in workload required to expose and use an API, and the flexibility of the end result. There are a lot of quirks and special syntaxes in Cython to allow for not just interop between C and Python, but also writing C in Python-like syntax mixed with actual Python. This is an area I assume this approach would be lacking.
Regardless, as someone who loves mixing C and Python, this is an encouraging reason to look more at D.
by jononor on 2/19/20, 2:40 PM
I often use C++ and pybind11 to do this. pybind11 takes an approach very similar to boost::python (template magic). It works rather well.
Though most of the C code I write can compile as C++ also.
Why write in C then? It's still the lingua franca in microcontroller land... Just want to reuse the code on PC and Embedded Linux, and Python makes high-level testing much nicer.
by dekhn on 2/19/20, 4:52 PM
It's really crazy how many ways there are to implement FFI in Python. I started in the "old days" (Makefile, a bunch of CPython C code to implement argument parsing, etc), switched to swig (experienced both the good and bad sides), hoped that GCC-XML would make it easier to convert complicated C++ classes to Python interfaces (big mistake), then hoped that LLVM 'would solve the problem', saw Google release CLIF (still can't compile it from scratch on a modern linux system), and the large number of "late-binding" approaches. Of all of them, I was most skeptical of Cython but have slowly come to appreciate its sublime cleverness.
by jaked89 on 2/19/20, 2:35 PM
D & Nim (especially Nim) are very appealing from a language perspective, but the lack an IDE undermines the advantages they bring.
I personally won't consider using a language for a meaningful project unless it has a decent debugger, and reasonable refactoring; ie. at least find references & rename.
Beef is building on an IDE, which is a refreshing take, but it's still too early.
by atilaneves on 2/19/20, 2:29 PM
I describe how easy it is to call C code from Python by using D as the glue layer. AMA.
by andrewprock on 2/19/20, 4:19 PM
Is swig no longer a thing in the 2020s?
I've always done this using the bundled Python/C integration path, or when there was too much stuff, use swig.
http://swig.org/
by marmaduke on 2/19/20, 7:32 PM
by skocznymroczny on 2/19/20, 2:34 PM
"Envious of C++’s and Objective C’s credible claim to be the only languages that can seamlessly interoperate with C (due to header inclusion and compatible syntax)"
Wouldn't Nim and Zig also fit the bill?
by abrax3141 on 2/19/20, 3:05 PM
Okay, but what if I want to call python libraries from C (or any other language)? What we need is some sort of .net-like global interop in the Unix world.
by fithisux on 2/20/20, 10:19 AM
Hopefully the dream to compile a D library to another language (something like swigd) it will be interesting
by justlexi93 on 2/20/20, 4:09 AM
Scripting languages by nature will always be slower than C. There's a tradeoff between program speed and development speed
by hatsunearu on 2/19/20, 2:28 PM
Or you know, learn any of the native options that let you call C things relatively painlessly.
by epicgiga on 2/19/20, 2:52 PM
How many languages do we need again? The amount of time we spend playing around with special case languages and frameworks. Would be nice if machine languages followed the lead of natural languages, with a single one slowly pushing the rest to the sidelines.