Jeff Schwab wrote: > How [EMAIL PROTECTED] wrote: >> Ruby has a neat little convenience when writing loops where you don't >> care about the loop index: you just do n.times do { ... some >> code ... } where n is an integer representing how many times you want >> to execute "some code." >> >> In Python, the direct translation of this is a for loop. When the >> index doesn't matter to me, I tend to write it as: >> >> for _ in xrange (1,n): >> some code >> >> An alternative way of indicating that you don't care about the loop >> index would be >> >> for dummy in xrange (1,n): >> some code >> >> But I like using _ because it's only 1 character and communicates well >> the idea "I don't care about this variable." >> >> The only potential disadvantages I can see are threefold: >> >> 1. It might be a little jarring to people not used to it. I do admit >> it looks pretty strange at first. >> >> 2. The variable _ has special meaning at the interactive interpreter >> prompt. There may be some confusion because of this. >> >> 5. Five is right out. (ob Holy Grail reference, of course. :-) >> >> So, I guess I'm wondering if anyone else uses a similar idiom and if >> there are any downsides to it that I'm not aw > > Would something like this be acceptable? It still requires a loop > variable, plus an extra line of code per loop, plus a one-time class > definition (and import into each client module), and it's probably > slower than "for dummy in range." The syntax might be more inuitive > than "dummy" or "_" in a for loop, though. > > class Go: > def __init__(self, count): > self.count = count > > def again(self): > if self.count <= 0: > return False > self.count -= 1 > return True > > go = Go(3) > while go.again(): > print "hello"
One could rename the again() method __call__(), allowing the even more horrible but less verbose while Go(3)(): print "hello" That'd confuse the newbies! regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list