arcpy.management.<gp_tool> vs. arcpy.<gp_tool>_management?

206
8
Jump to solution
08-04-2020 03:31 PM
dcafdg
by
Occasional Contributor

When I open the help in Pro from a GP tool, the docs (example) give me usage like this: 

arcpy.SelectLayerByAttribute_management( ... )


When I go to the GP history and copy the Python snippet (now called "Copy Python Command"), it gives me slightly different syntax: 

arcpy.management.SelectLayerByAttribute( ... )

The second looks much more like a Pythonic package structure to me. Is there any functional difference or any particular reason to use one over the over? 

Reply
0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

The historical/legacy syntax was arcpy.SelectLayerByAttribute_management, and the newer/current syntax is arcpy.management.SelectLayerByAttribute.  The underlying package structure of ArcPy hasn't really changed, Esri is just moving away from the function alias model they originally adopted.  Personally, I consider the newer syntax more Pythonic.

View solution in original post

8 Replies
DanPatterson
MVP Frequent Contributor

No functional difference... same script

arcpy.SelectLayerByAttribute_management??
Source:
@gptooldoc('SelectLayerByAttribute_management', None)
.... huge snip ....
File: c:\arc_pro\resources\arcpy\arcpy\management.py
Type: function

arcpy.management.SelectLayerByAttribute??
Source:
@gptooldoc('SelectLayerByAttribute_management', None)
.... huge snip ....
File: c:\arc_pro\resources\arcpy\arcpy\management.py
Type: function

... sort of retired...
JoshuaBixby
MVP Esteemed Contributor

If you are not using Jupyter, you can also see they are the same by using the code dunder attribute:

>>> arcpy.SelectLayerByAttribute_management.__code__
<code object SelectLayerByAttribute at 0x0000023D1BF5F1E0, file "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 8699>
>>>
>>> arcpy.management.SelectLayerByAttribute.__code__
<code object SelectLayerByAttribute at 0x0000023D1BF5F1E0, file "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 8699>
>>>
DanPatterson
MVP Frequent Contributor

It was actually Spyder... anything that can use ipython line magics will work (others include jupyterlab, notebook, jupyter console or Pro's notebooks)


... sort of retired...
dcafdg
by
Occasional Contributor

So I guess that begs the question, which is more Pythonic?

Reply
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

The historical/legacy syntax was arcpy.SelectLayerByAttribute_management, and the newer/current syntax is arcpy.management.SelectLayerByAttribute.  The underlying package structure of ArcPy hasn't really changed, Esri is just moving away from the function alias model they originally adopted.  Personally, I consider the newer syntax more Pythonic.

View solution in original post

RDHarles
Occasional Contributor

After 15 years they're going to switch the order?  I remember back in the day when you didn't even need the "toolbox" (e.g."_management") ~AND~  you could use any case you wanted (e.g. SeLeCtLaYeRbYaTTrIBuTe).

As a old timer, I don't like it...and get off my lawn! 

dcafdg
by
Occasional Contributor

Too bad there aren't laugh emoji reactions in GeoNet. Thanks for the chuckle, RD.

Reply
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

This is Jive's "laugh" emoji: