zip as iterator and bad/good practices

Fabien fabien.maussion at gmail.com
Fri Jun 12 11:00:09 EDT 2015


Folks,

I am developing a program which I'd like to be python 2 and 3 
compatible. I am still relatively new to python and I use primarily py3 
for development. Every once in a while I use a py2 interpreter to see if 
my tests pass through.

I just spent several hours tracking down a bug which was related to the 
fact that zip is an iterator in py3 but not in py2. Of course I did not 
know about that difference. I've found the izip() function which should 
do what I want, but that awful bug made me wonder: is it a bad practice 
to interactively modify the list you are iterating over?

I am computing mass fluxes along glacier branches ordered by 
hydrological order, i.e. branch i is guaranteed to flow in a branch 
later in that list. Branches are objects which have a pointer to the 
object they are flowing into.

In pseudo code:

for stuff, branch in zip(stuffs, branches):
	# compute flux
	...
	# add to the downstream branch
	id_branch = branches.index(branch.flows_to)
	branches[id_branch].property.append(stuff_i_computed)

So, all downstream branches in python2 where missing information from 
their tributaries. It is quite a dangerous code but I can't find a more 
elegant solution.

Thanks!

Fabien




More information about the Python-list mailing list