Like a for loop to calculate an argument

My question is very simple.

Does the loop check the forargument that it uses every time?

For instance:

for i in range(300):

Does python create a list of 300 elements for each iteration of this loop?

If so, is this a way to avoid this?

lst = range(300)
for i in lst:
    #loop body

The same goes for code examples like this.

for i in reversed(lst):

for k in range(len(lst)):

Is the inverse process used every time or the length calculated at each iteration? (I set this for both python2 and python3)

If not, how does Python evaluate changes to iterable during iteration over it?

+4
source share
3 answers

, . , :

it = iter(range(300))
while True:
    try:
        i = next(it)
    except StopIteration:
        break
    ... body of loop ...

, , break -. , else for, .

+7

, , __iter__ Iterable, , .

Python Iterable, . Python2 range , Iterable, __iter__, Iterator.

>>> from collections import Iterable, Iterator
>>> isinstance(range(300), Iterable)
True
>>> isinstance(range(300), Iterator)
False
>>> isinstance(iter(range(300)), Iterator)
True

for in sequence: do something :

it = iter(some_iterable) # get Iterator from Iterable, if some_iterable is already an Iterator, __iter__ returns self by convention
while True:
    try:
        next_item = next(it)
        # do something with the item
    except StopIteration:
        break

, , for:

class CapitalIterable(object):
    'when iterated over, yields capitalized words of string initialized with'
    def __init__(self, stri):
        self.stri = stri

    def __iter__(self):
        print('__iter__ has been called')
        return CapitalIterator(self.stri)

        # instead of returning a custom CapitalIterator, we could also
        # return iter(self.stri.title().split())
        # because the built in list has an __iter__ method

class CapitalIterator(object):
    def __init__(self, stri):
        self.items = stri.title().split()
        self.index = 0

    def next(self): # python3: __next__
        print('__next__ has been called')
        try:
            item = self.items[self.index]
            self.index += 1
            return item
        except IndexError:
            raise StopIteration

    def __iter__(self):
        return self

c = CapitalIterable('The quick brown fox jumps over the lazy dog.')
for x in c:
    print(x)

:

__iter__ has been called
__next__ has been called
The
__next__ has been called
Quick
__next__ has been called
Brown
__next__ has been called
Fox
__next__ has been called
Jumps
__next__ has been called
Over
__next__ has been called
The
__next__ has been called
Lazy
__next__ has been called
Dog.
__next__ has been called

, __iter__ , Iterator.

+4

Range 300 ints . 300 ints 300 . . xrange, , . https://docs.python.org/2/library/functions.html#xrange

example.py

for i in xrange(300): #low memory foot print, similar to a normal loop
    print(i)
0

Source: https://habr.com/ru/post/1629031/


All Articles