Does anyone else use this little idiom?

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sat Feb 2 22:38:21 EST 2008


En Sun, 03 Feb 2008 01:03:43 -0200, James Matthews <nytrokiss at gmail.com>  
escribió:

Sorry to be nitpicking, but people coming from other languages may get  
confused by the wrong examples:

> What i do is a simple range call. for i in range(number of times i want  
> to repeat something)
> I guess it comes from my C days for(i=0;i<100;i++) { or in python for i  
> in range(99):

Should be `for i in range(100)` to match exactly the C loop. Both iterate  
100 times, with i varying from 0 to 99 inclusive.

>>  miller.paul.w 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

Should be `for _ in xrange(n)` to match the Ruby example. Both iterate n  
times.

> On Feb 3, 2008 3:34 AM, Roy Smith <roy at panix.com> wrote:
>
>> But, more to the point, I'd try to find variable name which described  
>> why I was looping, even if I didn't actually use the value in theloop  
>> body:

Me too. Government don't collect taxes by the number of variable names  
used (yet).

-- 
Gabriel Genellina




More information about the Python-list mailing list