Having a function set a global variable
Question: Consider the following C++ module (explanation to follow):

    
    
    #include <Python.h>
    #include "nr3python.h"
    
    Doub tau_0;
    
    static PyObject* analysis_c_set_parameters(PyObject *self, PyObject *pyargs) {
      NRpyArgs args(pyargs);
      Doub tau_0 = NRpyDoub(args[0]);
      return NRpyObject();
    }
    
    Doub solver(const Doub t) {
      return tau_0;
    }
    
    static PyObject* analysis_c_solver(PyObject *self, PyObject *pyargs) {
      NRpyArgs args(pyargs);
      const Doub t = NRpyDoub(args[0]);
      const Doub result = solver(t);
      return NRpyObject(result);
    }
    
    static PyMethodDef AnalysisCMethods[] = {
      {"set_parameters", analysis_c_set_parameters, METH_VARARGS,
       "Set fixed parameters for the optimization procedure."},
      {"solver", analysis_c_solver, METH_VARARGS,
       "Call the optimization function for solving for the spike time."},
      {NULL, NULL, 0, NULL}
    };
    
    PyMODINIT_FUNC
    initanalysis_c(void)
    {
      (void) Py_InitModule("analysis_c", AnalysisCMethods);
    }
    

Note that this module is setup so that its functions can be imported into
Python scripts.

I have a global variable `tau_0`, which is declared without an initial value.
The value is set (or should be set) when the function
`analysis_c_set_parameters` is called (within Python). This value is then read
out when `analysis_c_solver` is called: `analysis_c_solver` calls `solver`,
which returns `tau_0`. `analysis_c_solver` then returns `tau_0`.

The problem I'm facing is that when I call these functions in Python script, I
get `0.0` as the value for `tau_0` even after I've attempted to set it to
something else (e.g., `0.5`).

In Python I have

    
    
    import analysis_c
    
    
    TAU_0 = 0.5
    t = 0
    
    analysis_c.set_parameters(TAU_0)
    actual_result = analysis_c.solver(t)
    

In this case, `actual_result` is set to `0.0`.

What's going on here?


Answer: This line declares and initialises a new local variable:

    
    
    Doub tau_0 = NRpyDoub(args[0]);
    

To assign to the global variable that you already declared you need to write

    
    
    tau_0 = NRpyDoub(args[0]);
    

