Mixing Python and C classes in a module

Kay Schluehr kay.schluehr at gmx.net
Tue Oct 9 17:18:13 CEST 2007


On Oct 9, 4:56 pm, Stefan Arentz <stefan.are... at gmail.com> wrote:
> Is it possible to mix classes defined in both Python and C in the same
> module? Ideally I would like to be able to do:
>
>  from some.module import MyPythonClass, MyCClass
>
> I guess that would mean that this would look like this on disk:
>
>  some/
>    __init__.py
>    module.py      (contains MyPythonClass)
>    module.so      (contains MyCClass)
>
> But would this work?
>
>  S.

Yes, but you need to write your own importer and customize it using
ihooks.py. The builtin imp module also contains a function new_module
that lets you allow creating a module without any file reference. This
can be returned containg the names of both module.py and module.so.




More information about the Python-list mailing list