[Python-ideas] having a "iterable" built-in

Ben Finney ben+python at benfinney.id.au
Thu Apr 30 02:57:58 CEST 2009


Cameron Simpson <cs at zip.com.au> writes:

> On 29Apr2009 20:47, Tarek Ziad? <ziade.tarek at gmail.com> wrote:
> | def iterable(ob):
> |     try:
> |         iter(ob)
> |     except TypeError:
> |         return False
> |     return True
> 
> This is actually a bad way of doing it. Suppose using the iterator has
> side effects?

If getting an iterator of an object has side effects, I'd say that
object's implementation is buggy.

> For example, "ob" might be a store-of-recent-messages, which empties
> after they have been collected. Or a file() attached to a pipe.

Neither of which should lose any items merely by getting an iterable.

The only thing that should cause the iterable to “lose” items is
calling its ‘next’ method, which never happens with Tarek's code above.

-- 
 \        “If you go parachuting, and your parachute doesn't open, and |
  `\        you friends are all watching you fall, I think a funny gag |
_o__)             would be to pretend you were swimming.” —Jack Handey |
Ben Finney




More information about the Python-ideas mailing list