On Thu, Mar 15, 2012 at 7:37 AM, Croepha <croe...@gmail.com> wrote: > Which is preferred: > > for value in list: > if not value is another_value: > value.do_something() > break > > --or-- > > if list and not list[0] is another_value: > list[0].do_something() > > Comments are welcome, Thanks
General principle: Make the code look like what it's doing. I don't mean text art and making code in the shape of pi that prints the digits of pi (although that can be awesome too), but more that a loop should not be used when you don't intend for it to loop. Consider the For-Case Paradigm[1] and the amazing confusion value that it can offer. A loop needn't execute more than once (it needn't even execute the first time), but it should at least have the _potential_ to execute the same code multiple times, otherwise it's hardly a loop. I had a particularly nasty example of a loop-that-wasn't-a-loop at work a while ago; it was PHP, not Python, so I won't share it here, but it had a do-while loop and an insidious bug in it. Very tricky. ChrisA [1] http://thedailywtf.com/Articles/The_FOR-CASE_paradigm.aspx -- http://mail.python.org/mailman/listinfo/python-list