ArcGIS Field Calculator – Meaning of Exclamation Mark in Python Parser

arcgis-desktopfield-calculatorpython-parser

In the following line, I don't understand what those exclamation marks mean? And why put everything in quotation?
I don't understand the purpose of the variable expression, please explain to me as I'm new to python

expression = "crossing(!CROSS_ROW!,!ROW_SCORE!)"

codeblock = """def crossing(crosses, row_score):
            if crosses != 0:
                return 5
            else:
                return row_score"""

The code gives a score to pipes (from pipe layer) that cross, or passes next to roads (from road layer) depending on the width of the road. this was the code-block that assigned a score of 5 if the pipe crosses.

Best Answer

What you are looking at is an Advanced Field Calculation.

It's a little confusing because you're kind of referencing it backwards. If you right-click on a field in an attribute table and select the field calculator, you'll notice an option in the field calculator window to change the parser to Python as well as a check box named 'Show Code Block'. When checked, the 'Show Code Block' check box allows you to perform an advanced field calculation by defining a custom function and using that function to calculate the values of the field.

Your Code Block would go into the Pre-Logic Script Code section as a custom function. Your Expression would go into the Field Calculation Expression section below that (In my case: Site_ID =)

enter image description here

So crossing() as defined in the Pre-Logic Script Code window is a function, which takes two parameters, crosses and row_score.

In the second window, the parameters (crosses and row_score) are being passed to the crossing() function by means of the values in the fields "CROSS_ROW" and "ROW_SCORE", respectively, for each record in the attribute table.

So in this function, the crossing() function will be executed against each value in the 'Site_ID' field. If the value held in the field "CROSS_ROW" for that particular record is not equal to 0, a value of 5 will be returned and populated into the 'Site_ID' field for that record. Otherwise, if the value held in the field "CROSS_ROW" is not equal to 0 (!=), then the value held in the field "ROW_SCORE" will be returned and populated into the 'Site_ID' field for that particular record.

EDIT: To more properly answer your question: encapsulating something in exclamation marks means value from the field named whatever is encapsulated. So the value of the field named !CROSSES! for this record. The reason everything is in quotes is because whoever wrote this was probably doing this through ESRI's ArcPy Site Package which would require this expressions and code blocks be passed in as a string.

Related Question