0

How can one surelly tell that function retuns an iterable object, which calculates results on demand, and not an iterator, which returns already calculated results?

For e.g. function filter() from python's documentation says:

Construct an iterator from those elements of iterable for which function returns true

Reading that I cat tell that this function returns an object which implements iterable protocol but I can't be sure it won't eat up all my memory if use it with generator which reads values from 16gb file untill I read further and see the Note:

Note that filter(function, iterable) is equivalent to the generator expression (item for item in iterable if function(item))

So, how does one can tell that function calculates returned results on demand and not just iterating over temporary lists which holds already calculated values? I have to inspect sources?

Javed
  • 326
  • 3
  • 8
  • I don't think you can. It's an implementation detail of the iterable, not something represented concretely. – Barmar Nov 07 '20 at 06:24
  • @Barmar oh, never thought about it like that. It makes sense, thank you! – Javed Nov 07 '20 at 06:27
  • Your distinction between them doesn't seem to be right. See https://stackoverflow.com/questions/2776829/difference-between-pythons-generators-and-iterators – Barmar Nov 07 '20 at 06:27
  • 1
    Iterators can also calculate results on demand. – Barmar Nov 07 '20 at 06:28
  • @Barmar you're righ, I fixed the question according to your remarks. Now it seems to me as a little documentation flaw. – Javed Nov 07 '20 at 06:38
  • 1
    Usually one just assumes that a well written library will not use an extremely inefficient method if there's an alternative. So it won't construct internal lists if it doesn't have to. – Barmar Nov 07 '20 at 06:42

1 Answers1

1

If the doc says that a function returns an iterator, it's pretty safe to assume it calculates items on the fly to save memory. If it did calculate all its items at once, it would almost certainly return a list.

luther
  • 5,195
  • 1
  • 14
  • 24