Why is "while" ticking me off???

Huaiyu Zhu hzhu at users.sourceforge.net
Fri Oct 6 13:40:00 EDT 2000


On Fri, 06 Oct 2000 16:13:41 +0100, Alan Gauld <alan.gauld at gssec.bt.co.uk>
wrote: 
>Thomas Gagne wrote:
>> while line = fp.readline():
>>     do something with 'line'
>> 
>> but that doesn't work because Python doesn't allow assignments 
>
>correct, so at the expense of typing a line twice, do 
>the assignment externally:

The expense could be high in real codes, not just a line or two.  See
http://www.geocities.com/huaiyu_zhu/python/ififif.txt

I quote an example here:

if val = dict1[key1]:
	process1(val)
elif val = dict2[key2]:
	process2(val)
elif val = dict3[key3]:
	process3(val)
...


The entirely equivalent python code would require nested ifs

val = dict1[key1]
if val:
	process1(val)
else:
	val = dict2[key2]
	if val:
		process2(val)
	else:
		val = dict3[key3]
		if val:
			process3(val)
		else:
			...

Huaiyu



More information about the Python-list mailing list