PyMaemo/Accessing APIs without Python bindings

(New page: = A brief introduction about ctypes = == Introduction == Sometimes you don't need a complete binding for some library, perhaps all you require is a couple functions that will solve your...)
Line 1: Line 1:
-
= A brief introduction about ctypes =
 
-
 
== Introduction ==
== Introduction ==

Revision as of 20:47, 27 January 2010

Introduction

Sometimes you don't need a complete binding for some library, perhaps all you require is a couple functions that will solve your problem or make your program faster. For these cases, you can use ctypes to cherry-pick the needed functions and use them right away.

Usage

Let's say you want to use libc's printf, for some reason. All you need in python is:

import ctypes
libc = ctypes.CDLL('libc.so.6')
libc.printf('Hello world!')

In a few words, you create an object correspondent to the library you need and use it to call the function directly. Things can get complicated if the parameters or the return type are complex structures, though.