Mixing Python and C classes in a module

Discussion in 'Python' started by Stefan Arentz, Oct 9, 2007.

  1. 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.
     
    Stefan Arentz, Oct 9, 2007
    #1
    1. Advertising

  2. Stefan Arentz

    Chris Mellon Guest

    On 09 Oct 2007 16:56:30 +0200, 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)
    >
    > But would this work?
    >


    No, you'll need to make module a package, and import from (differently
    named) implementation packages.
     
    Chris Mellon, Oct 9, 2007
    #2
    1. Advertising

  3. Stefan Arentz

    Kay Schluehr Guest

    On Oct 9, 4:56 pm, 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)
    >
    > 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.
     
    Kay Schluehr, Oct 9, 2007
    #3
  4. Stefan Arentz

    John Machin Guest

    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
     
    John Machin, Oct 9, 2007
    #4
  5. On 10/9/07, Chris Mellon <> wrote:
    > On 09 Oct 2007 16:56:30 +0200, 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)
    > >
    > > 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
     
    Nicholas Bastin, Oct 10, 2007
    #5
  6. Stefan Arentz

    Guest

    On Oct 10, 3:32 pm, "Nicholas Bastin" <> wrote:
    > On 10/9/07, Chris Mellon <> wrote:
    >
    >
    >
    > > On 09 Oct 2007 16:56:30 +0200, 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)

    >
    > > > 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 *
     
    , Oct 10, 2007
    #6
  7. Stefan Arentz

    John Machin Guest

    On 10/10/2007 1:02 PM, 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 :)
     
    John Machin, Oct 10, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    697
  2. Replies:
    1
    Views:
    551
    Greg Schmidt
    Dec 7, 2004
  3. Ivan Voras

    Mixing classes...

    Ivan Voras, Aug 19, 2004, in forum: Python
    Replies:
    1
    Views:
    293
    Hans Nowak
    Aug 20, 2004
  4. L.C. Rees
    Replies:
    3
    Views:
    391
    Scott David Daniels
    Feb 8, 2006
  5. snacktime
    Replies:
    1
    Views:
    117
    Nobuyoshi Nakada
    Sep 20, 2006
Loading...

Share This Page