Creating a Python script 'interface' to c/c++ module

Fredrik Lundh effbot at
Tue Mar 7 16:58:49 CET 2000

Makhno <mak at> wrote:
> Unfortuanately, I can't seem to provide the Python wrapper, the way the
> modules work does not seem to let me. For example, I want to call a C
> function called my_func(). With Perl I would use the hierarchy:
> Users code->Perl wrapper->C wrapper (XS file)->C function
> with Python, I can't seem to include the second step, only
> Users code->C wrapper->C function
> So calling my_module.my_func() in Python, calls C code directly, there is
> chance of executing any Python inbetween.
> The question is how do I get some Python code in there? The only way I can
> think of is writing a Python module which acts as a wrapper for the C
> module, which doesn't seem like the best way.

well, that's how everyone else does this.  if you don't
want to expose the C module directly, call it _mymodule
and provide a that wraps it.

note that you can let import everything
from _mymodule, and just override stuff as necessary:


    import _mymodule
    from _mymodule import *

    def myfunction(args):
        # override _mymodule.myfunction
        args = convert(args)
        result = _mymodule.myfunction(args)
        return process(result)

hope this helps!


<!-- (the eff-bot guide to) the standard python library:

More information about the Python-list mailing list