Part C, Part Python Module?

Alex Martelli aleax at aleax.it
Tue Feb 25 05:31:53 EST 2003


John Abel wrote:

> I am in the process of writing a module, and now need to code some parts
> of it in C.  Is it possible to have them both imported using the same
> name?  Or, will I have to import the C-based module in the Python module?

You can build the module object in C including whatever pieces you
want that are implemented in Python (by embedding the Python code
strings in your C sources, or reading them from some other file,
and executing them).  Or you can build the module object in Python
and add whatever names you want that come from another module that
is coded in C -- this is generally more flexible.

It's possible to "join" a module implemented in C and another one
implemented in Python in several ways, though the above two are the
main ones.  But some "joining" by explicit code must take place
somewhere or other.  It will never be the case that the statement:
    import onename
will by itself import TWO (or more) modules -- this statement will 
always import just ONE module (the body of that module, or function
init_onename if the module is C-coded, may of course add names to
the module object in whatever way it chooses to).


Alex





More information about the Python-list mailing list