Take Advantage of Keyword Arguments

This short blog explains and demonstrates that our arcpy and arcgisscripting modules support the use of keyword arguments and by using them; you can avoid long tool signatures. Keyword arguments are associated with function calls. In our arcpy and arcgisscripting modules, geoprocessing tools are considered functions. When you use keyword arguments in a function call, the caller identifies the arguments by the parameter name. This allows you to skip arguments or specify them in a different order. Let us look at two examples:

Example 1: Adding a Field

Optional arguments may be skipped using double quotes.

import arcpy
arcpy.AddField_management("schools", "school_id", "LONG", "", "", "", "", "NON_NULLABLE")

import arcgisscripting
gp = arcgisscripting.create()
gp.AddField_management("schools", "school_id", "LONG", "", "", "", "", "NON_NULLABLE")

As a better alternative, use keyword arguments. This code uses the parameter name and sets it equal to the value:

import arcpy
arcpy.AddField_management("schools", "school_id", "LONG", field_is_nullable="NON_NULLABLE")

import arcgisscripting
gp = arcgisscripting.create()
gp.AddField_management("schools", "school_id", "LONG", field_is_nullable="NON_NULLABLE")

To find parameter names, refer to the tool or function help topic in the ArcGIS Desktop Help. Optionally, you can use the help function in Python:

>>> help(arcpy.AddField_management)

Keyword argument support also applies to other functions. Here is an example of using the ListFeatureClasses() function:

Example 2: List Polygon Feature Classes

import arcpy
arcpy.env.workspace = "C:/Data/Redlands.gdb"
feature_classes = arcpy.ListFeatureClasses(feature_type="Polygon")

import arcgisscripting
gp = arcgisscripting.create()
feature_classes = gp.listfeatureclasses(feature_type="Polygon")

In sum, keyword arguments save time and make your code easier to read.

Remember, all optional parameters to Geoprocessing tools have default values. Therefore, if a value is not provided in the tool call for that argument, the default value is used.

This entry was posted in Python and tagged , . Bookmark the permalink.

Leave a Reply


  1. recurvata says:

    Pretty cool. Something I knew way back but never think of. Thanks.

  2. fulfilledstraw says:

    Great post, I’m looking forward to cleaning up my code. I keep getting a “ListFeatureClasses() got an unexpected keyword argument ‘feature_type’”, though. I can’t seem to figure out why. Any thoughts?

    • najuste says:

      If you just type ‘the keyword’ in the brackets is should work.. like gp.listfeatureclasses("Polygon")
      As I have noticed that ‘Keyword argument support’ is not actually supported on arcgis 9.3…

  3. hornbydd says:


    I find reading these blog pages very useful as I often discover functionality I never knew existed, for example this blog page about keyword arguments.

    I was recently trawling through the Help document and read about the new arcpy.da module for improved cursors. I was even more interested in the code examples they gave as they were using the “with” statement for controlling the release of the cursor which I never knew you could do.

    Unless I’ve missed it, what I’ve realised is that esri is missing a best coding practise blog/help page. It would be a REALLY useful page if one of your Python geo-processing gurus sat down and wrote a simple list of best techniques. Us developers spent half of our time debugging poor code (most likely written by themselves!) or deciphering cryptic error messages. If we were using best practises when dealing with the esri geo-processing objects then I’m sure locking issues, invalid parameters would be a little less painful.