AttributeError with arcpy.AddMessage when printing field object defaultValue property

312
1
Jump to solution
04-27-2023 04:18 AM
Clubdebambos
Occasional Contributor III

ArcGIS Pro 3.1 (also same error tested on 2.9)

Getting an AttributeError when using arcpy.AddMessage() to print the default values for fields.

AttributeError: Object: Error in parsing arguments for AddMessage

Original code...

import arcpy

## path to feature class
fc = r"C:\Path\To\GDB\FeatureClass"

## describe the data and return a dictionary
desc = arcpy.da.Describe(fc)

## for each field object in the fields property
for field_obj in desc["fields"]:
    ## print the field name
    arcpy.AddMessage(field_obj.name)
    ## print the field defaultValue
    arcpy.AddMessage(field_obj.defaultValue)
~ learn.finaldraftmapping.com
0 Kudos
1 Solution

Accepted Solutions
Clubdebambos
Occasional Contributor III

Figured it out shortly after original post.

ArcPy does not like a None value as input parameter for the AddMessage() function, replaced with string formatting below.

 

import arcpy

## path to feature class
fc = r"C:\Path\To\GDB\FeatureClass"

## describe the data and return a dictionary
desc = arcpy.da.Describe(fc)

## for each field object in the fields property
for field_obj in desc["fields"]:
    ## print the field name
    arcpy.AddMessage(field_obj.name)
    ## print the field defaultValue
    arcpy.AddMessage("{0}".format(field_obj.defaultValue))

 

 The defaultValue property was returning None for some fields. If you run the below it will throw the AttributeError, seen in original post, showing that the input to the AddMessage function must be convertible to a string.

 

## AttributeError
arcpy.AddMessage(None)

 

 

~ learn.finaldraftmapping.com

View solution in original post

1 Reply
Clubdebambos
Occasional Contributor III

Figured it out shortly after original post.

ArcPy does not like a None value as input parameter for the AddMessage() function, replaced with string formatting below.

 

import arcpy

## path to feature class
fc = r"C:\Path\To\GDB\FeatureClass"

## describe the data and return a dictionary
desc = arcpy.da.Describe(fc)

## for each field object in the fields property
for field_obj in desc["fields"]:
    ## print the field name
    arcpy.AddMessage(field_obj.name)
    ## print the field defaultValue
    arcpy.AddMessage("{0}".format(field_obj.defaultValue))

 

 The defaultValue property was returning None for some fields. If you run the below it will throw the AttributeError, seen in original post, showing that the input to the AddMessage function must be convertible to a string.

 

## AttributeError
arcpy.AddMessage(None)

 

 

~ learn.finaldraftmapping.com