Mutual module imports

Tony Nelson *firstname*nlsnews at georgea*lastname*.com
Tue Oct 18 05:11:20 CEST 2005


How does one normally make a Python extension module that has some parts 
in Python and some functions in C share globals between the Python and C 
functions?  Will that approach work with Pyrex?

I have written a Python module that uses some C functions.  I wrote the 
module in two parts, one Python, one Pyrex (C).  They need to share some 
globals.  (I use pyrex to handle ref counting.  I think I'm glad I did.)

At first they just sort of mutually imported each other, and it worked 
until I put tests in the Python one and set it up to run them when it is 
named "__main__".  What happened reminded me that there are also other 
ways modules can be imported under different names, so I tried a 
different approach.

Now the Python module imports the Pyrex module and just shoves 
references to its globals into the Pyrex module (the Pyrex module 
defines them as None).  The Pyrex module doesn't import the Python 
module anymore.  This also works, even when the Python module has a 
different name (e.g. "__main__").  I just feel dirty about it.
________________________________________________________________________
TonyN.:'                        *firstname*nlsnews at georgea*lastname*.com
      '                                  <http://www.georgeanelson.com/>



More information about the Python-list mailing list