I suggest that using the same name to refer to different variables in the same function is bad practice. Even in C. There are plenty of other bad practices in C that you can't do in python, for example failing to indent.
My point is that the C case is undesirable. I concede that this is acceptable:
for (int i = 0; i < 42; i++) { ... }
for (int i = 0; i < 37; i++) { ... }
But having an outer version of i
as well would be confusing.
Those kinds of loops are mostly used in C for iterating through arrays. In the example i
is an int
, but it could be a pointer that is being incremented.
Counting loops using range()
are not needed nearly as much in python because we have object iterators.
A frustrated C programmer might do this in python:
for i in range(len(mylist)):
print(mylist[i])
but the following is preferred:
for list_item in mylist:
print(list_item)
So my second point is that the C case is unnecessary.
The philosophy in Python is based on objects and object iterators. In C you don't have that. In C myarray[i]
is syntactic sugar for *(myarray + i)
, we just don't want that in python, we have many more different types of containers than C and iteration should be consistent.
I'm not saying you never need the index number, but there is always enumerate()
.