Does anyone else use this little idiom?

Arnaud Delobelle arnodel at googlemail.com
Sun Feb 3 04:20:32 EST 2008


On Feb 3, 2:03 am, miller.pau... at gmail.com 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
[...]

If 'some code' is a function (say f) you can write (with repeat from
itertools):

for action in repeat(f, n): action()

I don't know how 'Pythonic' this would be...

--
Arnaud




More information about the Python-list mailing list