Constant variable and API

mg mg.mailing-list at laposte.net
Thu Sep 30 03:56:52 EDT 2004


Hi everybody...

I am using Python API in order to create bindings.
So, in the init function of my module, I create constants :

   PyMODINIT_FUNC initMyModule( void )
   {
      PyObject* module = Py_InitModule3( "MyModule", 0, "this is my 
module" ) ;
      if( ! module ) return ;

      PyObject* dict = PyModule_GetDict( module ) ;
      if( ! dict ) return ;

      long value = 0 ; // for the example
      PyObject* py_value = PyInt_FromLong( value ) ;
      char* name = "NULL" ;
      PyDict_SetItemString( dict, name, py_value ) ;
      Py_DECREF( py_value ) ;
   }

So, my first "problem" is that my module variable is mutable; I can 
write the following instruction in python :

   >>> import MyModule
   >>> null = MyModule.NULL
   >>> print null
   0
   >>> MyModule.NULL = 99
   >>> null = MyModule.NULL
   >>> print null
   99

Then, my question is : how can I implement a constant variable from the 
API in order to the reaffectation (MyModule.NULL = 99) of my variable be 
impossible ?

Thanks for your help.
Mathieu



More information about the Python-list mailing list