473,473 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Mixing Python and C classes in a module


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.
Oct 9 '07 #1
6 2627
On 09 Oct 2007 16:56:30 +0200, Stefan Arentz <st***********@gmail.comwrote:
>
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.
Oct 9 '07 #2
On Oct 9, 4:56 pm, Stefan Arentz <stefan.are...@gmail.comwrote:
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.

Oct 9 '07 #3
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
Oct 9 '07 #4
On 10/9/07, Chris Mellon <ar*****@gmail.comwrote:
On 09 Oct 2007 16:56:30 +0200, Stefan Arentz <st***********@gmail.comwrote:

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
Oct 10 '07 #5
On Oct 10, 3:32 pm, "Nicholas Bastin" <nick.bas...@gmail.comwrote:
On 10/9/07, Chris Mellon <arka...@gmail.comwrote:
On 09 Oct 2007 16:56:30 +0200, Stefan Arentz <stefan.are...@gmail.comwrote:
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 '07 #6
On 10/10/2007 1:02 PM, ti******@gmail.com 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 :-)
Oct 10 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

54
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
0
by: Erik Max Francis | last post by:
Is there any prohibition against mixing different protocols within the same pickle? I don't see anything about this in the Python Library Reference and, after all, the pickle.dump function takes a...
7
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
4
by: Rudolf | last post by:
Is it possible to add a vb.net source code module to a c# project and if so how? Thanks Rudolf
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 349 open ( +7) / 3737 closed (+25) / 4086 total (+32) Bugs : 939 open (-12) / 6648 closed (+60) / 7587 total (+48) RFE : 249 open...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.