Mixing Python and C classes in a module

timaranz at gmail.com timaranz at gmail.com
Tue Oct 9 23:02:56 EDT 2007


On Oct 10, 3:32 pm, "Nicholas Bastin" <nick.bas... at gmail.com> wrote:
> On 10/9/07, Chris Mellon <arka... at gmail.com> wrote:
>
>
>
> > On 09 Oct 2007 16:56:30 +0200, 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?
>
> > No, you'll need to make module a package, and import from (differently
> > named) implementation packages.
>
> Nah, you can do it, just not in this way.  You can either futz with
> ihooks, or name module.py something like _module.py, import it into C,
> and then re-export as 'module', after attaching all your C types.
>
> --
> Nick

It is easier to do it the other way around.
Create module.py and _module.so and in module.py write:

from _module.so import *




More information about the Python-list mailing list