Why doesn't this loop break?

前端 未结 6 1398
太阳男子
太阳男子 2021-01-16 00:01

Python noob; please explain why this loop doesn\'t exit.

for i in range(0,10):
  print \"Hello, World!\"
  if i == 5: i = 15
  print i
next

相关标签:
6条回答
  • 2021-01-16 00:06

    Why would it exit? You're iterating over elements of range(0, 10), the only exit condition is that your run out of elements. Rebinding the name doesn't change anything inside the iterable. If you want to break prematurely, use break.

    for i in range(0, 10):
        if i == 5: break
    
    0 讨论(0)
  • 2021-01-16 00:15

    Because what you have done with range(0,10) is created an array of 10 elements like so:

    1, 2, 3, 4, 5, 6, 7, 8, 9, 10
    

    and you are going through each one.

    In other programming languages, you are doing what is called a foreach loop.

    Otherwise, do it another way.

    0 讨论(0)
  • 2021-01-16 00:15

    Because for i in whatever means that for each element in whatever, the loop body will be executed once setting i to that element. There is no rule that says that at the end of each iteration, i must still be an element of whatever.

    Reassigning i in the loop body has no effect whatsoever on how often the loop body executes or which value i will hold in the next iteration.

    0 讨论(0)
  • 2021-01-16 00:17

    Because Python's for loop doesn't count, it iterates. range produces an iterable (a list in Python 2.x), and that iterable is iterated over. Each time before the loop body is executed, the next item is pulled from it, and no changes you make in the loop body to the iteration variable affect the memory of what the next value is supposed to be. That's saved elsewhere and you generally can't affect it. Simply use break.

    0 讨论(0)
  • 2021-01-16 00:21

    In python the for loop is not a while loop like in C or Java; it's instead an iteration over an iterable.

    In you case (assuming Python 2.x) range(0, 10) (that by the way is the same as range(10)) is a function that returns a list. A python list is an iterable and can therefore be used in a for loop... what the language will do is assigning the variable to first, second, third element and so on and each time it will execute the body part of for.

    It doesn't matter if you alter that variable in the body... Python will just pick next element of the list until all elements have been processed. Consider that your loop is not really different from

     for i in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
         print "Hello, world."
         if i == 5: i = 15
         print i
    

    that in turn is also quite similar to

     for i in [1, 29, 5, 3, 77, 91]:
         print "Hello, world."
         if i == 5: i = 15
         print i
    

    why should an assignment to i break the loop?

    0 讨论(0)
  • 2021-01-16 00:25

    You are iterating over a range object, which is just a collection of values ranging from 0...10. There is no logic behind it, as it is just a collection of numbers.

    The for item in set syntax essentially just loops over every element in the set, breaking only when every item has been looped over.

    Your code is not equivalent to a C++ for loop, which would produce your desired result:

    for (int i = 0; i < 10; i++) {
      if (i == 5) {
        i = 15;
      }
    }
    

    If you want to break out of the loop, just break the loop:

    for i in range(0,10):
      print "Hello, World!"
      if i == 5: break
      print i
    
    0 讨论(0)
提交回复
热议问题