arcpy.da.SearcherCursor() as Cursor or as Row

2290
5
Jump to solution
09-08-2017 12:33 PM
JoeBorgione
MVP Emeritus

What is the difference in declaring a search cursor as a cursor or a row?  Is it just a style/personal thing or is there a difference in functionality?  

In Adding to a Selection , Curtis Price‌ uses as row in his solution for me, in SearchCursor—Help | ArcGIS Desktop as cursor is used. There seems to be a slight syntax difference used between the two when accessing the information in the cursor itself:

with arcpy.da.SearchCursor("apLayer", "FullStreet") as rows:
    streets = [row[0] for row in rows]   .......

versus

with arcpy.da.SearchCursor(fc, fields) as cursor:

     for row in cursor:

           blah blah blah blah.......

That should just about do it....
0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

Looking at the Python aspect of your question, both of your examples use the with statement:

The with statement is used to wrap the execution of a block with methods defined by a context manager (see section With Statement Context Managers). This allows common tryexceptfinally usage patterns to be encapsulated for convenient reuse.

with_stmt ::=  “with” with_item (“,” with_item)* “:” suite
with_item ::=  expression [“as” target]

The part after as, whether called "rows" or "cursor," it just a label/name for the target object, and that label/name carries no meaning in and of itself.  You could write, with arcpy.da.SearchCursor() as elephant:, and it wouldn't matter; but it might confuse someone reading the code what a cursor has to do with elephants.

The part of the code after the with statement in your examples, the suite as the documentation refers to it, doesn't depend on whether someone used "rows" or "cursors," they are just names.  For example, one doesn't have to use "rows" to use a list comprehension in the suite, list comprehensions could be used with whatever named was chosen for the target object of the context manager.

View solution in original post

5 Replies
VinceAngelo
Esri Esteemed Contributor

Both are valid. The first is just a naming preference for working with list comprehensions.

In the absence of thorough documentation, a coherent naming policy can self-document many a script. There often times when a code cascade raises conflicts with standard naming, at which point you need to do the best you can. Remember: The nice thing about standards is that you have so many to choose from.

- V

JoeBorgione
MVP Emeritus

Yep...  Words to (I) live by....

That should just about do it....
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Looking at the Python aspect of your question, both of your examples use the with statement:

The with statement is used to wrap the execution of a block with methods defined by a context manager (see section With Statement Context Managers). This allows common tryexceptfinally usage patterns to be encapsulated for convenient reuse.

with_stmt ::=  “with” with_item (“,” with_item)* “:” suite
with_item ::=  expression [“as” target]

The part after as, whether called "rows" or "cursor," it just a label/name for the target object, and that label/name carries no meaning in and of itself.  You could write, with arcpy.da.SearchCursor() as elephant:, and it wouldn't matter; but it might confuse someone reading the code what a cursor has to do with elephants.

The part of the code after the with statement in your examples, the suite as the documentation refers to it, doesn't depend on whether someone used "rows" or "cursors," they are just names.  For example, one doesn't have to use "rows" to use a list comprehension in the suite, list comprehensions could be used with whatever named was chosen for the target object of the context manager.

JoeBorgione
MVP Emeritus

Thanks Josua!

bixb0012

That should just about do it....
0 Kudos
VinceAngelo
Esri Esteemed Contributor

I want to debug code that contains:

with arcpy.da.SearchCursor(featureclassname, featureclassfieldnames) as antidisestablishmentarianisms:

    for antidisestablishmentarianism in antidisestablishmentarianisms:

        blah

- V