Select to view content in your preferred language

Tuples showing as list in arcpy Message

791
4
Jump to solution
12-05-2022 07:26 AM
BrianLeroux
Frequent Contributor

I have been trying to add coordinate into a tuple so I can create a new layer but for some reason the the tuples show as lists when i output them to arcpy.addMessage. Running the following code below gives me output of 3 lists. Anyone know what could be going on here?

Start Time: Monday, December 05, 2022 10:20:47 AM
[1,2,3,4]
[1,2,3,4]
[2,4,6,8]
Succeeded at Monday, December 05, 2022 10:20:47 AM (Elapsed Time: 0.08 seconds)

 

 

mylist = [1,2,3,4]
arcpy.AddMessage(mylist)

#convert list to tuple
mytuple = tuple(mylist)
arcpy.AddMessage(mytuple)

my2ndTuple = (2,4,6,8)
arcpy.AddMessage(my2ndTuple)

 

 

1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

Regarding this latter issue, i.e., AddMessage not working with type, it seems like a defect to me.  If you use AddError or AddWarning it works as expected.  It seems AddMessage does not like having any brackets in the string, so any string with one gets returned as empty.

 

UPDATE:  If you pad str(type(my2ndTuple)) with a space at the start and end so the string doesn't start or end with an angle bracket, it works.

View solution in original post

4 Replies
BrianLeroux
Frequent Contributor

Also trying to get the type does not work. I get no output from the following.

arcpy.AddMessage(type(my2ndTuple))

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Regarding this latter issue, i.e., AddMessage not working with type, it seems like a defect to me.  If you use AddError or AddWarning it works as expected.  It seems AddMessage does not like having any brackets in the string, so any string with one gets returned as empty.

 

UPDATE:  If you pad str(type(my2ndTuple)) with a space at the start and end so the string doesn't start or end with an angle bracket, it works.

BrianLeroux
Frequent Contributor

This is helpful as now I can see it is truly creating my tuple and not somehow converting it to a list. It is strange that even though it is a tuple the display of a tuple in messages and warnings shows a tuple in brackets and not parentheses. Definitely makes things confusing but I think the fist issue i have is now not really an issue. 

I might open a couple of bugs for the message display issues.

I appreciate the help here.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

ArcPy AddMessage will display the expected result if you create the string yourself and then pass it to AddMessage.  For example, don't do arcpy.AddMessage(tuple), instead do arcpy.AddMessage(str(tuple)).  I would say this is another defect, but my overall suggestion is to always build the string you want and then call AddMessage because AddMessage is really about passing back informational messages, i.e., text.