Define a constant in Python C extension

Robert Kern robert.kern at gmail.com
Fri Feb 27 22:03:39 CET 2009


On 2009-02-27 14:54, fredbasset1000 at gmail.com wrote:
> I'm writing a C extension for Python, is it possible to define
> constants in the C code and have them visible from Python?

In your init<module> function, create Python objects from the constants 
and insert them into the module using PyModule_AddObject(). The 
initspam() function in this section does this (though not with #defined 
constants):

http://docs.python.org/extending/extending.html#providing-a-c-api-for-an-extension-module

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though 
it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list