Loops/Iteration in Python – Geoprocessing

Having tried to learn Python by reading Python books rather than ESRI help on Python, I always thought that the looping/iteration with .Next() that I kept seeing in scripts online looked funny, or redundant, or just annoying.

fcs = gp.ListFeatureClasses() 
fc = fcs.Next()  
while fc: 
   # Do something
   fc = fcs.Next()

More Pythonic, I would’ve written it like this:

fcs = gp.ListFeatureClasses() 
for fc in fcs: 
   # Do something

And I understand from Michalis Avraam’s page here (where I saw the above code samples) that as of version 9.3, the way to go is the Pythonic one. Great !



Filed under Uncategorized

2 responses to “Loops/Iteration in Python – Geoprocessing

  1. Arne I completely agree the first example does look strange. If I remember correctly it is written this way because gp.ListFeatureClasses() returns an enumeration in 9.2 while in 9.3 it returns a real List. I am not yet on 9.3, two more weeks, but I cant wait to stop with the whole .Next() stuff.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s