Creating a "package" using C extensions?

Gordon McMillan gmcm at
Sun Dec 9 13:53:47 EST 2001

Courageous wrote:

[wants to implement a package in C]
>>You could probably hack something up by butchering the 
>>module object in some way, but the import code assumes that extension
>>modules are just plain modules, not packages. 
> This is liveable, but somewhat painful for me. Let me tell
> you why. I am currently translating the back end of a system
> originally written in Python to a hybridized C++/Python
> implementation with a C++ core. The heart of the system has
> a very high invocation frequency, so it makes sense to put
> that into C++. However, I need to make the prior interface
> to the Python programmer the same as it was before. So what
> I am doing is, for every module in the prior package, creating
> a C++ equivalent.
> That's a lot of .dlls, if you know what I mean.
> Perhaps I'll break down and write a makefile. Doing a couple
> dozen .dlls in VC 6.0 is a bit of a pain.
> It's too bad there's not a way to create a package-level .dll
> which responds to Python in such a way as to offer up its
> internal modules, but all from within a single .dll. That would
> be cool.

Oh. If that's what you want, you asked the wrong question! Except
for a few corner cases, there's no difference between package.module
and module.attribute. Once the import is done, it's all 
object.attribute anyway. And when importing, there's no difference
(except implementation, of course) between package.module and 

So implement <package>.c as your extension module, and arrange
for it to have module1, module2 etc. as attributes. Probably a
dozen or two lines of perfectly kosher code in init<package>.

-- Gordon

More information about the Python-list mailing list