[Python-ideas] Introduce collections.Reiterable

Ben Finney ben+python at benfinney.id.au
Sat Sep 21 06:54:50 CEST 2013

Neil Girdhar <mistersheik at gmail.com>

> However, there are plenty of times where you can't do that, e.g., you
> want to know if something is callable before calling it

What is a concrete example of *needing* to know whether an object is
callable? Why not just use the object *as if it is* callable, and the
TypeError will propagate back to whoever fed you the object if it's not?

> and similarly if something is reiterable before iterating it and
> exhausting.

I have somewhat more sympathy for this desire; duck typing doesn't work
so well for this, because by the time the iterable is exhausted it's too
late to deal with its inability to re-start.

Still, though, this is the kind of division of responsibility that makes
a good program: tell the user of your code (in the docstring of your
class or function) that you require a sequence or some other re-iterable
object. If you try something that fails on what object you've been
given, that's the responsibility of the code that gave it to you. You
can be nice by ensuring it'll fail in such a way the caller gets a
meaningful exception.

 \        “The number of UNIX installations has grown to 10, with more |
  `\         expected.” —Unix Programmer's Manual, 2nd Ed., 1972-06-12 |
_o__)                                                                  |
Ben Finney

More information about the Python-ideas mailing list