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?
Solved! Go to Solution.
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.
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
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>
>>>
It was actually Spyder... anything that can use ipython line magics will work (others include jupyterlab, notebook, jupyter console or Pro's notebooks)
So I guess that begs the question, which is more Pythonic?
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.
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!
Too bad there aren't laugh emoji reactions in GeoNet. Thanks for the chuckle, RD.
This is Jive's "laugh" emoji: