To me with my beginners knowledge of Python, it seems a bit unclean, and out of place for a precise and tightly controlled feature set language like Python. Let's see if I am proven wrong when I know more.
Edit on 4/8:
As expected, I was proven wrong. Turns out there is a very useful common idiom that is addressed by this. We typically code a "did_it_happen_flag" in a for or a while loop (pseudocode)
This can be coded in Python like this:
This way that extra check is not required - if the break were never hit, the else associated with the while takes care of taking the action we need.
Edit on 4/8:
As expected, I was proven wrong. Turns out there is a very useful common idiom that is addressed by this. We typically code a "did_it_happen_flag" in a for or a while loop (pseudocode)
while(...) {
if(...) {
did_it_happen = TRUE;
break;
}
}
if(!did_it_happen) {
printf("It didn't :-(");
// Do something useful
}
This can be coded in Python like this:
while (...):
if(...) : break
...
else:
print "It didn't :-("
This way that extra check is not required - if the break were never hit, the else associated with the while takes care of taking the action we need.
No comments:
Post a Comment