I think fundamentally by special-casing a for-loop variant, you have a
construct with limited/no generality that's simply an additional burden to
learn. You're kind of doing the opposite of converting print from a
statement into a function. I far prefer the print function because it's a
function like every other and doesn't have it's own curious syntax (if only
assert was also fixed..., assert(cond, reason) has bit me a few times)

It's nice that you can put *anything* that supports the iterator protocol
after "for x in" and Python will "iterate" over it, however that is defined
for the object. The thing might not have a useful integer index at all
(dictionaries, file systems...) and the loop doesn't care. If you *do* want
that index, you can enumerate() or range(len()), which I sometimes do if
modifying a list in-place, but it's kinda icky as David suggests.

Nick

On Fri, Feb 17, 2017 at 4:31 PM, David Mertz <me...@gnosis.cx> wrote:

> Iterating over range(len(collection)) is one of the worst anti-patterns in
> Python. I take great pains to slap my students who do that.
>
> On Feb 17, 2017 9:32 AM, "Mikhail V" <mikhail...@gmail.com> wrote:
>
>>
>> On 17 February 2017 at 17:37, Chris Angelico <ros...@gmail.com> wrote:
>>
>>> On Sat, Feb 18, 2017 at 3:30 AM, Mikhail V <mikhail...@gmail.com> wrote:
>>> > On 17 February 2017 at 04:59, Chris Angelico <ros...@gmail.com> wrote:
>>> >>
>>> >> On Fri, Feb 17, 2017 at 2:13 PM, Mikhail V <mikhail...@gmail.com>
>>> wrote:
>>> >> > Common use case:
>>> >> >
>>> >> > L = [1,3,5,7]
>>> >> >
>>> >> > for i over len(L):
>>> >> >    e = L[i]
>>> >> >
>>> >> > or:
>>> >> >
>>> >> > length = len(L)
>>> >> > for i over length:
>>> >> >    e = L[i]
>>> >>
>>> >> Better use case:
>>> >>
>>> >> for i, e in enumerate(L):
>>> >>
>>> >
>>> > This would be more compact, yet less readable, more error prone
>>> variant.
>>> > I'd avoid it it all costs and even if I don't need the index further
>>> in loop
>>> > body,
>>> > (which happens rarely in my experience) I write e=L[i] in second line
>>> > to make the code more verbose and keep the flow order.
>>> > So your variant (and those proposed in PEP-212) could serve in list
>>> > comprehensions for example, but for common 'expanded' code  I find it
>>> > decline of readability, and creating totally different variants for
>>> same
>>> > iteration idea.
>>> > But partially that could be simply matter of habit and love to
>>> contractions.
>>>
>>> If you don't need the index, why not just iterate over the list directly?
>>>
>>> for e in L:
>>>
>>> That's the single most obvious way to step through a collection in
>>> Python. What do you need to count up to the length for?
>>>
>>>
>> I have said I need the index, probably you've misread my last comment.
>> Further more I explained why I think iteration over index should be the
>> preferred way, it help with readability a lot.
>> All my learning years ended up with rewriting most code to "for i in
>> range()"
>> and I slap myself when I start to write "for e in L".
>> It is exactly where TOOWTDI applies perfectly and it is integer iteration
>> for me.
>>
>> Mikhail
>>
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas@python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to