Using a variable before definition in user-defined function

I see. In this case I think the simplest solution would be to 1) define one function for each group and 2) define all of them in the end, before the run function. If it is too much of a mess to not define the functions in the beginning, you could also manually define variable names instead of using device.get_array_name: the name of the spike monitor’s count variable is brian::_array_spikemonitor_count, with spikemonitor the name of the SpikeMonitor object. If you manually define these names with SpikeMonitor(..., name='myname') for each monitor, you can directly put this name into the code.

Note that if all you need from the spike monitor is the spike count (we probably discussed this before, apologies :slight_smile: ), then it would probably be easier to just have this information directly represented in the neuron equations, with something like spike_count : integer in the equations, and a spike_count += 1 as part of the reset.