Select to view content in your preferred language

Cursors

1050
2
10-29-2009 01:45 PM
TedCronin
MVP Alum
Is the support for FOR loops new at 9.4, it seems like I could never get them to work with anything but a while loop.  :confused:
0 Kudos
2 Replies
DavidWynne
Esri Contributor
Hi Ted,
Right, this is an enhancement at 9.4.  Less code, easier to understand.


At 9.3, you would have to use a while loop like so...

rows = gp.SearchCursor(myTable)
row = rows.next()
while row:
    print row.getValue("Rank")
    row = rows.next()

At 9.4, you can you use a for loop (you can also use the while loop approach if you desire)...

for row in arcpy.SearchCursor(myTable)
    print row.getValue("Rank")


-Dave
0 Kudos
TedCronin
MVP Alum
Hi Ted,
Right, this is an enhancement at 9.4.  Less code, easier to understand.


At 9.3, you would have to use a while loop like so...

rows = gp.SearchCursor(myTable)
row = rows.next()
while row:
    print row.getValue("Rank")
    row = rows.next()

At 9.4, you can you use a for loop (you can also use the while loop approach if you desire)...

for row in arcpy.SearchCursor(myTable)
    print row.getValue("Rank")


-Dave


Absofrickenlutely, much more pythonic then while loops.  This is HUGE.
0 Kudos