I have in my columns attribute the following table ( follows the image):

enter image description here

"rank" (which is related to the order of my data);

"SL" (which is a linear measure of my object);

"testada" (the formula):

enter image description here

I wanted a help in the implementation of the formula tested in the field command of the calculator arcgis.

The columns "rank" and "SL" range, containing more or less attributes. That is, My data vary and what I'm showing is just a sample of my data set so was wanting a more dynamic formula. This formula has to read up to the last line, and return the sum of tested according to formula.

  • 1
    What is the initial value if testdata? (first row)
    – fatih_dur
    Oct 11, 2016 at 22:09

2 Answers 2


You can use a python "closure" construct or a global var, but I prefer not to use globals.


calc_testada( !SL! )

Code block:

def testada(initval=0):
    #Remember state using a "closure" pattern
    #Note closure vars are read-only in Python 2x
    #So we use a mutable var like a list
    val = [initval]

    def _testada(SL):
       val[0] += SL
       return val[0]

    return _testada

calc_testada = testada()

enter image description here


After setting your parser to Python and selecting Show Codeblock (inside Pre-Logic Script Code):

testada = 0 #Assuming your initial value for this is zero, i.e. testada[i-1] for rank 1

def sum_value (val):
    global testada
    testada += val
    return testada

And paste the following code in the smaller box below the Pre-Logic Script Code

  • +1 from me. I said in my answer I prefer not to use globals, but that's just in general as I usually write code for larger scripts/libraries where global vars can lead to problems. In this case a global variable won't cause any issues and makes for simpler code than what I posted.
    – user2856
    Oct 13, 2016 at 3:57
  • As you said, the use of global(s) is context dependent and for a very simple process like this, the interpreter will deal with only one global (and once) and yield.
    – fatih_dur
    Oct 13, 2016 at 4:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.