having a "iterable" built-in

Hello unless I missed it, I couldn't find a built-in to check if an object is iterable, so I wrote this function : def iterable(ob): try: iter(ob) except TypeError: return False return True What about having such a built-in in Python ? (with the proper implementation if course) Regards Tarek -- Tarek Ziadé | http://ziade.org

Tarek Ziadé a écrit :
Well, I guess the new abilities of abstract base classes and stuffs, in 2.6, allow this : http://docs.python.org/library/collections.html You should call something like isinstance(ovj, Iterable), which is less cute than isiterable(obj), but far more flexible ^^ Regards, Pascal

On 29Apr2009 20:47, Tarek Ziad? <ziade.tarek@gmail.com> wrote: | unless I missed it, I couldn't find a built-in to check if an object | is iterable, | | so I wrote this function : | | 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? For example, "ob" might be a store-of-recent-messages, which empties after they have been collected. Or a file() attached to a pipe. -- Cameron Simpson <cs@zip.com.au> DoD#743 http://www.cskk.ezoshosting.com/cs/ Morning people may be respected, but night people are feared. - Bob Barker <Bob.Barker@MCI.Com>

Cameron Simpson <cs@zip.com.au> writes:
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

On 30Apr2009 10:57, Ben Finney <ben+python@benfinney.id.au> wrote: | Cameron Simpson <cs@zip.com.au> writes: | > On 29Apr2009 20:47, Tarek Ziad? <ziade.tarek@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. Oh. Sorry, my bad. You're absolutely right. I was confusing getting the iterator with using it. Thanks! -- Cameron Simpson <cs@zip.com.au> DoD#743 http://www.cskk.ezoshosting.com/cs/ ...if you don't get the finger several times a day [while commuting in Boston], you're not driving correctly. - Mark Parrenteau, DJ, WBCN Boston

I don't think that's going to happen. The similar builtin "callable" has been removed as of Python 3.0 in favour of isinstance(x, collections.Callable), or in versions < 2.6, hasattr(x, '__call__'), and the convention is similar for iterables ("isinstance(x, collections.Iterable)" or "hasattr(x, '__iter__')"). On 29 Apr 2009, at 14:47, Tarek Ziadé wrote:

On Thu, Apr 30, 2009 at 12:20 AM, Terry Reedy <tjreedy@udel.edu> wrote:
hasattr(ob, '__iter__')
Right, that works under Python 3, (It won't cover str under Python 2 though) Tarek -- Tarek Ziadé | http://ziade.org

Tarek Ziadé schrieb:
The old-style iteration protocol (__getitem__ and __len__) has not disappeared in Python 3. It's just that str got its own iterator type. Georg -- Thus spake the Lord: Thou shalt indent with four spaces. No more, no less. Four shall be the number of spaces thou shalt indent, and the number of thy indenting shall be four. Eight shalt thou not indent, nor either indent thou two, excepting that thou then proceed to four. Tabs are right out.

Tarek Ziadé a écrit :
Well, I guess the new abilities of abstract base classes and stuffs, in 2.6, allow this : http://docs.python.org/library/collections.html You should call something like isinstance(ovj, Iterable), which is less cute than isiterable(obj), but far more flexible ^^ Regards, Pascal

On 29Apr2009 20:47, Tarek Ziad? <ziade.tarek@gmail.com> wrote: | unless I missed it, I couldn't find a built-in to check if an object | is iterable, | | so I wrote this function : | | 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? For example, "ob" might be a store-of-recent-messages, which empties after they have been collected. Or a file() attached to a pipe. -- Cameron Simpson <cs@zip.com.au> DoD#743 http://www.cskk.ezoshosting.com/cs/ Morning people may be respected, but night people are feared. - Bob Barker <Bob.Barker@MCI.Com>

Cameron Simpson <cs@zip.com.au> writes:
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

On 30Apr2009 10:57, Ben Finney <ben+python@benfinney.id.au> wrote: | Cameron Simpson <cs@zip.com.au> writes: | > On 29Apr2009 20:47, Tarek Ziad? <ziade.tarek@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. Oh. Sorry, my bad. You're absolutely right. I was confusing getting the iterator with using it. Thanks! -- Cameron Simpson <cs@zip.com.au> DoD#743 http://www.cskk.ezoshosting.com/cs/ ...if you don't get the finger several times a day [while commuting in Boston], you're not driving correctly. - Mark Parrenteau, DJ, WBCN Boston

I don't think that's going to happen. The similar builtin "callable" has been removed as of Python 3.0 in favour of isinstance(x, collections.Callable), or in versions < 2.6, hasattr(x, '__call__'), and the convention is similar for iterables ("isinstance(x, collections.Iterable)" or "hasattr(x, '__iter__')"). On 29 Apr 2009, at 14:47, Tarek Ziadé wrote:

On Thu, Apr 30, 2009 at 12:20 AM, Terry Reedy <tjreedy@udel.edu> wrote:
hasattr(ob, '__iter__')
Right, that works under Python 3, (It won't cover str under Python 2 though) Tarek -- Tarek Ziadé | http://ziade.org

Tarek Ziadé schrieb:
The old-style iteration protocol (__getitem__ and __len__) has not disappeared in Python 3. It's just that str got its own iterator type. Georg -- Thus spake the Lord: Thou shalt indent with four spaces. No more, no less. Four shall be the number of spaces thou shalt indent, and the number of thy indenting shall be four. Eight shalt thou not indent, nor either indent thou two, excepting that thou then proceed to four. Tabs are right out.
participants (7)
-
Adam Atlas
-
Ben Finney
-
Cameron Simpson
-
Georg Brandl
-
Pascal Chambon
-
Tarek Ziadé
-
Terry Reedy