Skip to main content

45. ctypes Module

ctypes lets you call functions in DLLs or shared libraries directly from Python.


Example

// square.c
int square(int x) { return x * x; }

Compile to shared library (.so or .dll).

import ctypes

lib = ctypes.CDLL("./square.so")
print(lib.square(5))

Wrap-Up

ctypes provides a simple way to interface with C code without writing extensions.