B
B
Bjornie2018-03-19 01:27:23
Django
Bjornie, 2018-03-19 01:27:23

When is iterators (iter/next) needed in practice in Python?

The mechanics of creating an iterator and its use are clear to me, but it is not clear how in practice I may need to iterate manually using next (or the __next__ method). I'm a Django developer, basically. How can this be useful to me? And in other tasks, not very difficult - where?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey Gornostaev, 2018-06-18
@Bjornie

If you have code that needs to loop through a QuerySet that might have millions of records, then the usual

for item in some_qs.all():
    ...

can be dangerous as all data will be loaded into memory at once. The iterator will load data from the database in small blocks, and the objects will be returned one at a time, without caching them
for item in some_qs.iterator():
    ...

R
Ruslan., 2018-03-19
@LaRN

An example is lazy operations, such as getting data from a file. You can either upload everything at once into memory (if memory allows), or receive data in portions through an iterator.
But as for me, iterators allow you to make the code smaller and more concise. For example, the same zip and map allow complex constructions to fit into a small code.

A
Alexander, 2018-03-19
@Survtur

__next__()I often need when I need to take the first element of a generator. For example, a list of files and folders in a directory . I think a situation may arise when, when iterating, when analyzing the current value, it will be necessary to skip the next value (not the current one, but the next one). This is where it will help.
__next__()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question