Pyderman
Pyderman

Reputation: 16199

"Functions that consume an entire iterable won't terminate"?

In David Beazley's talk on generators, he states, as a caveat:

Functions that consume an entire iterable won't terminate(min, max, sum, set etc.)

What is meant here?

gen = (x*2 for x in [1,2,3,4,5])

sum(gen) terminates just fine.

Upvotes: 2

Views: 76

Answers (5)

Dimitris Fasarakis Hilliard
Dimitris Fasarakis Hilliard

Reputation: 160447

He's refering to that within the concept of infinite sequences, if you provide an infinite sequence to max et al, they simply won't be able to return a value.

Want to replicate? Aside from building a custom infinite sequence, Python has a built-in set of these in itertools (namely repeat, count, cycle). Try and do:

from itertools import repeat

max(repeat(20))

and see what happens. Actually, don't do that, max will keep on munching away as repeat keeps on giving numbers1. It's a love afair that lasts the challenge of time and never terminates :-)


1 -- Imagine Pac-Man in a never ending straight-line; constantly eating those little yellow thingies. Pac-Man = max, yellow thingies generated by repeat.

Upvotes: 5

Mike Müller
Mike Müller

Reputation: 85482

An endless generator won't terminate when consumed:

def gen():
    while True:
        yield 1

sum(gen())

Note: Don't actually execute the last line.

Upvotes: 3

Ariadni
Ariadni

Reputation: 145

He is talking about infinite itertors many of which can be found in itertools for Python. If you use an infinite iterator with them they won't return.

Upvotes: 1

Tadhg McDonald-Jensen
Tadhg McDonald-Jensen

Reputation: 21453

In the provided document the comment is directed towards the follow function on page 39 which is designed to lock up the program until the file is added to, any infinite generator will not terminate when used with functions that use an iterable.

Upvotes: 2

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798744

If you pay attention, you'll note that he writes that in Part 5 of the presentation, "Processing Infinite Data". Since infinite generators yield an infinite number of items, functions that attempt to consume the entire generator will never return.

Upvotes: 4

Related Questions