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.......
Solved! Go to Solution.
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 commontry
…except
…finally
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.
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
Yep... Words to (I) live by....
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 commontry
…except
…finally
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.
I want to debug code that contains:
with arcpy.da.SearchCursor(featureclassname, featureclassfieldnames) as antidisestablishmentarianisms:
for antidisestablishmentarianism in antidisestablishmentarianisms:
blah
- V