On infinite sequences

One of the cool features of the Python language is the notions of generators. Basically you can define a function that yields a sequence of values, and those values are generated on demand. The classical example is the code to generate the sequence of Fibonacci numbers:

def fibonacci():
  a = 0
  b = 1
  while True:
    a, b = b, a + b
    yield a

You can then use the fibonnaci() function to generate sequences of Fibonacci numbers of arbitrary length. While this example is not very useful in production code, the itertools package contains many useful generators, like repeat() which always returns the same value. The problem is, many of Python’s built-ins are not designed to cope with generators, for instance the min built-in cannot be used safely with generators: min(itertools.repeat(3)) runs forever, which is stupid, because the answer is trivial. Some built-ins, like len() actually try to call method __len__ of the object it is passed. If min and max were implemented in the same way, you could define it for many generators. I found some messages around that issues in the mailing lists of Python in 2000, but it does not look like it made it into the codebase, a shame.

4 thoughts on “On infinite sequences

  1. You should perharps take a look at the distinction between an Iterable and an Iterator. This is where the devil is…

  2. The problem is not in the difference between an iterable or an iterator. Python’s built ins min and max do not look at any particular properties of either…

  3. Well, yes, but this is also a problem of logic : Python clearly doesn’t have the facilities that exists more complex systems, and this is by design. But what if you could change things ? What would you propose ? Using a language like Python is clairly not the same experience than using a full workshop like C++, and you should think differently. For the question that has spawned this post ( min(repeat(3)) seems to loop endlessly) I have still the question “what are you trying to do ?”, because to my not-anymore-software-oriented eyes, it looks strange from the start…

    Also, since you have started to work with Python, I have only heard complains so it became old a while ago. You have probably features in the language that you like ? No ?

  4. The feature I like in Python are generators, because I think this is the only thing that is really special with Python. The real shame is that the language itself does not rely so much on them, Python 3000 finally makes a lot code behave like it should (i.e. in the way of itertools). The current problem is that generators make it very easy to work with infinite sequences, which is cool, but also many of the built in and core libraries are not designed to cope with such objects. The mathematical truth is that you do always need to go over all elements of a sequence to get some of the properties of that sequence, in the same that you do not request all the elements of a sequence to know its length.

Leave a Reply to edomaurCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.