top | item 37613416

(no title)

joss82 | 2 years ago

`ctypes` is part of Python's standard library and allows you to directly call C functions from Python code.

It's glorious in its simplicity.

https://docs.python.org/3/library/ctypes.html

discuss

order

masklinn|2 years ago

"Glorious" and "simplicity" are definitely words I've never read about ctypes before.

"Wonky" and "terrifying", a lot more. ctypes is... useful, but it also uses somewhat strange terminology which can be hard to match to C's as it's trying to bridge C and Python. And when getting it wrong is an UB, it's pretty frustrating.

jofer|2 years ago

It also doesn't help you use numpy arrays with C functions, which is one of the big selling points of cython.

ali_m|2 years ago

You can absolutely use numpy arrays with C functions using ctypes. Numpy has `numpy.ctypeslib` which takes care of some of the boilerplate involved.