*Naming Conventions*

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Wed Jun 6 13:26:46 CEST 2007

Neil Cerutti a écrit :
> On 2007-06-04, Michael Hoffman <cam.ac.uk at mh391.invalid> wrote:
>> Wildemar Wildenburger wrote:
>>> While that is true, I guess it is commonplace to use i, j, k
>>> and n (maybe others) in constructs like
>>> for i in range(len(data)):
>>>    do_stuff(data[i])
>>> Or should the good python hacker do that differently? Hope not
>>> ;).
>> Well, yes, I would do:
>> for item in data:
>>     do_stuff(item)
>> or, if using enumerate:
>> for item_index, item in enumerate(data):
>>     do_stuff(item_index, item)
>> I agree with Bruno that i and j should be used only for
>> indices, but I'm usually less terse than that.
> I find i and j preferable to overly generic terms like "item."

Since 'i' and 'j' are canonically loop indices, I find it totally 
confusing to use them to name the iteration variable - which is not an 
index. At least, 'item' suggests that it's an object, and a part of the 
collection - not just an index you'll have to use to subscript the 
container. Also, and as far as I'm concerned, I certainly dont find 'i' 
and 'j' *less* generic than 'item' !-)

I agree that except for uber-generic code or quick throw-away script 
'item' is probably not a very good name, but then nor are 'i' and 'j'...

More information about the Python-list mailing list