Does range() not evaluate its argument every time?

前端 未结 3 870
我寻月下人不归
我寻月下人不归 2021-01-15 11:22

l is passed as an argument to range function whose value is modified inside for loop, but the loop is going for 10 times

3条回答
  •  忘掉有多难
    2021-01-15 12:06

    No, the for loop evaluates the iterable expression just once.

    range() is called once, and the for loop then iterates over the result.

    Quoting from the for statement documentation:

    The expression list is evaluated once; it should yield an iterable object.

    emphasis mine.

提交回复
热议问题