title: Define a global in a Python module from a C API
body: <p>I am developing a module for Python using a C API. How can I create a variable that is seen as global from Python? </p>
  
  <p>For example, if my module is <code>module</code>, I want to create a variable <code>g</code> that does this job:</p>
  
  <pre><code>import module
  print module.g
  </code></pre>
  
  <p>In particular, <code>g</code> is an integer.</p>
  
  <h1>Solution from Alex Martelli</h1>
  
  <pre><code>PyObject *m = Py_InitModule("mymodule", mymoduleMethods);
  PyObject *v = PyLong_FromLong((long) 23);
  
  PyObject_SetAttrString(m, "g", v);
  Py_DECREF(v);
  </code></pre>