Making Calculate Field default to Python

Blog Post created by curtvprice Champion on May 11, 2015

It really is a bit annoying that Calculate Field tool's default (VBScript) parser does not work in x64. Here's a function that saves you typing when you want to default to the PYTHON_9.3 parser. Some Python scripts run this tool many times, so this little shortcut could make your script that much more readable.


import arcpy
def CalcField(tbl, field, expr, parser="PYTHON_9.3", codeblock=""):
    """Calculate Field with default Python parser"""
     CalculateField_management(tbl, field, expr, parser, codeblock)


How to use the helper function:


# Original syntax
arcpy.CalculateField_management("test.dbf", "ZFIELD", "!YFIELD! * 100", "PYTHON_9.3")
# Using the helper function
CalcField("test.dbf", "ZFIELD", "!YFIELD! * 100")


Why PYTHON_9.3 instead of PYTHON?

The PYTHON_9.3 is pretty much the same (performance-wise) as the PYTHON, but the difference comes in when a code block is used.  In the 9.3  [and later] geoprocessor ("PYTHON_9.3"), arcpy.List* functions return Python lists, not enumerators, and arcpy tools and methods return arcpy Result objects (not strings). This is especially important if your code block needs to work with geometry objects which are much easier to manipulate as objects instead of string representations.