I just want a record count from a Search Cursor.
If I write a line like this
nom = len(list(arcpy.da.SearchCursor(pointLayer, fields, whereClause)))
do I have to worry about deleting the cursor for cleanup? And if so how?
Or is something like this safer?
with arcpy.da.SearchCursor(pointLayer, fields, whereClause) as cursor:
denom = len(list(cursor))
Or if I stick the cursor in a function will it release it when the function is done?
Thanks a lot
with is usually good since it ensures that things get closed properly when done or things go wrong
As a little test, run your code snippet in your python IDE, then see if cursor still exists
>>> locals().keys() # is it there?
I get this
['nom', 'pointLayer', 'surveyGeo', 'whereClause', '__builtins__', 'fields', '__file__', '__package__', 'sys', 'arcgis', 'time', '__name__', 'datetime', 'arcpy', '__doc__', 'math']
But with no name what am I looking for?
If there are no references to the object, garbage collection will eventually take care of it.
Regarding counting records, there have been numerous discussions over the years on Esri forums, GeoNet, StackExchange, etc... about what are the "best" and "fastest" ways:
You should look into using
Get Count—Data Management toolbox | ArcGIS Desktop
the point layer is still there, there is no cursor and the point layer is closed by the with statement.
Experiment with a large file, one that would take a second or two to process and monitor memory usage.