How to write a py file with Assembly or C?

Viewed 32

I was reading Python's Pygame module and came across something similar; "Most functions in pygame are written using C and Assembly."How can they do that?

Can I do something like that?

import zNumber  #zNumber module is written with Assembly

a=zNumber.sum(1,2)
print(a)  #Out 3

2 Answers

You can import ctypes:

It provides C compatible data types, and allows calling functions in DLLs or shared libraries. It can be used to wrap these libraries in pure Python.

And this is documentation on how to write extensions.

Or you can write C extensions with Cython. Also checkout pyembedc for embedding C code.

How can they do that?

Regarding C, it is possible to write modules in C for CPython, how you could do that is explained in official python docs. Note that this requires lot of effort to make it work well. CFFI was developed to make calling C code from python easier.

Related