Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Mixing Python and C classes in a module

Reply
Thread Tools

Mixing Python and C classes in a module

 
 
Stefan Arentz
Guest
Posts: n/a
 
      10-09-2007

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.
 
Reply With Quote
 
 
 
 
Chris Mellon
Guest
Posts: n/a
 
      10-09-2007
On 09 Oct 2007 16:56:30 +0200, Stefan Arentz <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Kay Schluehr
Guest
Posts: n/a
 
      10-09-2007
On Oct 9, 4:56 pm, Stefan Arentz <(E-Mail Removed)> 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.

 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      10-09-2007
On 10/10/2007 12:56 AM, Stefan Arentz 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)
>


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
 
Reply With Quote
 
Nicholas Bastin
Guest
Posts: n/a
 
      10-10-2007
On 10/9/07, Chris Mellon <(E-Mail Removed)> wrote:
> On 09 Oct 2007 16:56:30 +0200, Stefan Arentz <(E-Mail Removed)> 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
 
Reply With Quote
 
timaranz@gmail.com
Guest
Posts: n/a
 
      10-10-2007
On Oct 10, 3:32 pm, "Nicholas Bastin" <(E-Mail Removed)> wrote:
> On 10/9/07, Chris Mellon <(E-Mail Removed)> wrote:
>
>
>
> > On 09 Oct 2007 16:56:30 +0200, Stefan Arentz <(E-Mail Removed)> 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 *

 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      10-10-2007
On 10/10/2007 1:02 PM, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
mixing C extension classes with ruby classes snacktime Ruby 1 09-20-2006 02:27 PM
Mixing custom __setattr__ method and properties in new style classes L.C. Rees Python 3 02-08-2006 05:35 PM
Mixing C library code and callbacks with C++ Classes womanontheinside@yahoo.com C++ 4 12-09-2004 11:42 PM
Mixing C library code and callbacks with C++ Classes womanontheinside@yahoo.com C++ 1 12-07-2004 07:31 PM
PYTHON API : add new classes in a module from an other module mathieu gontier Python 1 09-01-2004 12:35 PM



Advertisments