Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulating C-style for loops in python

Tags:

(even the title of this is going to cause flames, I realize)

Python made the deliberate design choice to have the for loop use explicit iterables, with the benefit of considerably simplified code in most cases.

However, sometimes it is quite a pain to construct an iterable if your test case and update function are complicated, and so I find myself writing the following while loops:

val = START_VAL while <awkward/complicated test case>:     # do stuff     ...     val = <awkward/complicated update> 

The problem with this is that the update is at the bottom of the while block, meaning that if I want to have a continue embedded somewhere in it I have to:

  • use duplicate code for the complicated/awkard update, AND

  • run the risk of forgetting it and having my code infinite loop

I could go the route of hand-rolling a complicated iterator:

def complicated_iterator(val):     while <awkward/complicated test case>:          yeild val          val = <awkward/complicated update>  for val in complicated_iterator(start_val):     if <random check>:          continue # no issues here     # do stuff 

This strikes me as waaaaay too verbose and complicated. Do folks in stack overflow have a simpler suggestion?

Response to comments:

@Glenn Maynard: Yes, I dismissed the answer. It's bad to write five lines if there is a way to do it in one... especially in a case that comes up all the time (looping being a common feature of Turing-complete programs).

For the folks looking for a concrete example: let's say I'm working with a custom date library. My question would then be, how would you express this in python:

for (date = start; date < end; date = calendar.next_quarter_end(date)):     if another_calendar.is_holiday(date):        continue     # ... do stuff... 
like image 277
10 revs, 7 users 53% Avatar asked Apr 29 '10 21:04

10 revs, 7 users 53%


People also ask

Can you do C style for loops in Python?

for in Loop: For loops are used for sequential traversal. For example: traversing a list or string or array etc. In Python, there is no C style for loop, i.e., for (i=0; i<n; i++). There is “for in” loop which is similar to for each loop in other languages.

What is the best alternative to a for loop in Python?

The map() function is a replacement to a for a loop. It applies a function for each element of an iterable.

Why doesn't Python have normal for loops?

The answer to your question is "Because they're different languages". They're not supposed to work alike. If they worked alike, they'd be the same language.

Does Python have a traditional for loop?

Python doesn't have traditional for loops. Let's see a pseudocode of how a traditional for loop looks in many other programming languages. The initializer section is executed only once, before entering the loop. The condition section must be a boolean expression.


1 Answers

This is the best I can come up with:

def cfor(first,test,update):     while test(first):         yield first         first = update(first)  def example(blah):     print "do some stuff"     for i in cfor(0,lambda i:i<blah,lambda i:i+1):         print i     print "done" 

I wish python had a syntax for closured expressions.

Edit: Also, note that you only have to define cfor once (as opposed to your complicated_iterator function).

like image 112
David X Avatar answered Oct 20 '22 01:10

David X