Re: if iter(iterator) is iterator

2016-11-13 Thread eryk sun
On Mon, Nov 14, 2016 at 6:25 AM, Serhiy Storchaka wrote: > On 14.11.16 02:40, Steve D'Aprano wrote: >> >> I'm surprised that the inspect module doesn't appear to have isiterable >> and isiterator functions. Here's my first attempt at both: > > Just use isinstance() with collections ABC classes. E

Re: if iter(iterator) is iterator

2016-11-13 Thread Serhiy Storchaka
On 14.11.16 02:40, Steve D'Aprano wrote: I'm surprised that the inspect module doesn't appear to have isiterable and isiterator functions. Here's my first attempt at both: Just use isinstance() with collections ABC classes. -- https://mail.python.org/mailman/listinfo/python-list

Re: if iter(iterator) is iterator

2016-11-13 Thread Steven D'Aprano
On Monday 14 November 2016 16:04, Chris Angelico wrote: > True, but ISTR there being a possibility that __iter__ could raise to > indicate that this isn't actually iterable. Maybe I'm recalling wrong. If __iter__ raises, that's a bug :-) Raising an exception from __iter__ appears to just fall th

Re: if iter(iterator) is iterator

2016-11-13 Thread Chris Angelico
On Mon, Nov 14, 2016 at 3:54 PM, Steven D'Aprano wrote: >> Any particular reason to write it that way, rather than: >> >> def isiterable(obj): >> try: >> iter(obj) >> return True >> except TypeError: >> return False > > > class BadIterable: > def __iter__(self):

Re: if iter(iterator) is iterator

2016-11-13 Thread Steven D'Aprano
On Monday 14 November 2016 11:59, Chris Angelico wrote: > On Mon, Nov 14, 2016 at 11:40 AM, Steve D'Aprano > wrote: >> def isiterable(obj): >> """Return True if obj is an iterable, and False otherwise. >> >> Iterable objects can be iterated over, and they provide either >> an __iter__

Re: if iter(iterator) is iterator

2016-11-13 Thread Chris Angelico
On Mon, Nov 14, 2016 at 11:40 AM, Steve D'Aprano wrote: > def isiterable(obj): > """Return True if obj is an iterable, and False otherwise. > > Iterable objects can be iterated over, and they provide either > an __iter__ method or a __getitem__ method. > > Iteration over an object

Re: if iter(iterator) is iterator

2016-11-13 Thread Steve D'Aprano
On Mon, 14 Nov 2016 07:02 am, Antoon Pardon wrote: > > Some time ago I read a text or saw a video on iterators and one thing > I remember from it, is that you should do something like the following > when working with iterators, to avoid some pitt falls. > > def bar(iterator): > if iter(iter

Re: if iter(iterator) is iterator

2016-11-13 Thread Chris Angelico
On Mon, Nov 14, 2016 at 8:57 AM, Gregory Ewing wrote: > Antoon Pardon wrote: > >> def bar(iterator): >> if iter(iterator) is iterator: >> ... > > > That's a way of finding out whether you can safely iterate > over something more than once. If the object is already an > iterator, applyi

Re: if iter(iterator) is iterator

2016-11-13 Thread Gregory Ewing
Antoon Pardon wrote: def bar(iterator): if iter(iterator) is iterator: ... That's a way of finding out whether you can safely iterate over something more than once. If the object is already an iterator, applying iter() to it will return the same object, and iterating over it will c

Re: if iter(iterator) is iterator

2016-11-13 Thread Chris Angelico
On Mon, Nov 14, 2016 at 7:02 AM, Antoon Pardon wrote: > Some time ago I read a text or saw a video on iterators and one thing > I remember from it, is that you should do something like the following > when working with iterators, to avoid some pitt falls. > > def bar(iterator): > if iter(itera