2008-10-29 13:45:42

Iterating hot air

Python allows to create iterators through the builtin function iter. This function has two ways of being called:

  • iter(collection) -> iterator
  • iter(callable, sentinel) -> iterator

The first call is rather unproblematic for sure. It calls a class method, it gets its object and that's it.

The second variant is however utterly useless. The callable is not called with any object — in fact, the call to iter does not even know what object is being iterated. This requires the programmer to store the state externally in some kind of global variable, or not to have any state at all. The latter would render the iterator rather pointless, since a crucial part of the purpose of an iterator is to externalize the state.

Posted by Tonnerre Lombard | Permanent link | File under: broken, programming