Why is "while" ticking me off???

Huaiyu Zhu hzhu at yahoo.com
Tue Oct 10 05:17:55 EDT 2000


On 9 Oct 2000 16:53:51 -0700, Aahz Maruch <aahz at panix.com> wrote:

>OIC.  Well, in Python, I'd write this as
>
>if dict[k1]:
>  dict[k1].something()
>
>assuming I were certain that k1 existed in dict, which you'd have to do
>in C (null pointer bugs are so much fun).

This is not entirely equivalent, as dict.__getitem__ may have side effects.
Besides, things like

if a=dict[k1]; a>0:
   print a, a+b
elif a=file.readline().strip(); a:
   print format % a
else:
   raise "Can't get a"
print "Finally we got a=", a

would be hard to do in a flat structure, IMHO, without puting statements in
if conditions.  But as I said, the most logical structure might be

a=dict[k1]; if a>0:
   print a, a+b
else: a=file.readline().strip(); if a:
   print format % a
else:
   raise "Can't get a"
print "Finally we got a=", a

This is in fact quite readable in colored editors.  It would also satisfy
someone's desire to separate elif into else if.

I was in fact hoping that someone could come up with a clever way to do this
in current Python without nested structures ...


Huaiyu



More information about the Python-list mailing list