How do I specify optional arguments in a geoprocessing tool?

Here is another common question I get asked quite a bit within the “Introduction to geoprocessing scripts using Python” course:

When I am calling a geoprocessing tool from within arcpy how can I specify an optional argument?

So, when running a geoprocessing tool within your script you must supply all mandatory arguments, at the very least for the tool to run. You can also supply additional optional arguments if you need to. Traditionally you had to make sure that these optional arguments were supplied in a strict order as stipulated by the tool’s syntax.

Let’s have a look at the Make Feature Layer tool found in the Data Management Toolbox. This is a useful tool that allows you to create a feature layer which can then be used in further geoprocessing tools such as Select Layer By Location. It also has a few optional arguments which you can specify.

Looking at the help for the tool you can see that it is made up of mandatory arguments (arguments which must be specified at the very least for the tool to begin execution), and optional arguments, specified by curly brace brackets {}.


MakeFeatureLayer_management (in_features, out_layer, {where_clause}, {workspace}, {field_info})

There are times when I would like to use the field_info optional parameter as I might wish to rename an attribute field found in my input layer to a new attribute field in my resulting feature layer. As you can see from looking at the tool’s syntax, the field_info parameter is the fifth argument in the tool’s parameter list and the third optional parameter.

So how do you make sure that the optional field_info parameter is passed into the tool at its correct position?

There are two main ways in achieving this:

1: Maintain order

The first way of specifying that I wish to use the field_info parameter is by creating placeholders for the previous unrequired optional arguments. The placeholders can be one of the following: an empty set of quotation marks (“”), a hash sign inside of a string (“#”) or by using the None type.

For example:

fldInfo = arcpy.FldInfo()
fldInfo.AddField("PC", "PostCode", "VISIBLE", "NONE")

The disadvantages of this approach is that additional code is required to display these placeholders and in a tool which has many optional parameters (for example Topo to Raster in the Spatial Analyst toolbox) it is quite easy to lose one’s position within the argument list.

2: Use keyword arguments

A much neater way of overcoming the specifying of the placeholders is to only include the optional arguments you want by fully qualifying the optional argument with its parameter name, for example:

field_info = fldInfo

The syntax for the Make Feature Layer tool would now read:

                                  field_info = fldInfo)

The advantages of this approach are that:

1: You are only including the optional arguments within the tool’s argument list

2: It is easier to see what arguments are included in the argument list

3: Your code is much more readable

Please note that this notation can only be used if you are an ArcGIS for Desktop 10.1 user, or above. If you are working with ArcGIS for Desktop 10.0 then you will have to use the notation in which you specify placeholders.

and there you have it!

** EDIT ** Many thanks to Jason Scheirer ( @jasonscheirer ) for pointing out a previous mistake in this post.