Mixing Python and C classes in a module

S

Stefan Arentz

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.
 
C

Chris Mellon

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.
 
K

Kay Schluehr

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.
 
J

John Machin

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)

Alternative to other suggestions:

Instead of module.so, call it _module.so. Then down the end of module.py:

try:
from _module import *
except ImportError:
# either panic or shrug as appropriate

Note: the "shrug" response would be appropriate for the use case where
"module" contains all necessary functionality, and the
optionally-present "_module" contains replacement classes/types and/or
functions that are better in some sense e.g. more memory/CPU efficient.

HTH,
John
 
N

Nicholas Bastin

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.
 
T

timaranz

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.

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 *
 
J

John Machin

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 *

It is even easier to type
from _module import *
and somewhat more productive, too :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top