Re: Iterators membership testing

2015-08-10 Thread Tim Chase
On 2015-08-09 19:24, Chris Angelico wrote: > That's exactly right. The only way for the interpreter to handle > 'in' on an iterator is something like this: > > def contains(iter, obj): > for val in iter: > if val == obj: return True > return False Which can nicely be written as

Re: Iterators membership testing

2015-08-09 Thread Mark Lawrence
On 09/08/2015 12:30, Laura Creighton wrote: Maybe add something about this here? https://docs.python.org/2/tutorial/classes.html#iterators Laura Better still https://docs.python.org/3/tutorial/classes.html#iterators -- My fellow Pythonistas, ask not what our language can do for you, ask what

Re: Iterators membership testing

2015-08-09 Thread Mark Lawrence
On 09/08/2015 14:11, Chris Angelico wrote: On Sun, Aug 9, 2015 at 11:09 PM, Tim Chase wrote: On 2015-08-09 19:24, Chris Angelico wrote: That's exactly right. The only way for the interpreter to handle 'in' on an iterator is something like this: def contains(iter, obj): for val in iter:

Re: Iterators membership testing

2015-08-09 Thread Tim Chase
On 2015-08-09 19:24, Chris Angelico wrote: > That's exactly right. The only way for the interpreter to handle > 'in' on an iterator is something like this: > > def contains(iter, obj): > for val in iter: > if val == obj: return True > return False Which can nicely be written as

Re: Iterators membership testing

2015-08-09 Thread Chris Angelico
On Sun, Aug 9, 2015 at 11:09 PM, Tim Chase wrote: > On 2015-08-09 19:24, Chris Angelico wrote: >> That's exactly right. The only way for the interpreter to handle >> 'in' on an iterator is something like this: >> >> def contains(iter, obj): >> for val in iter: >> if val == obj: return

Re: Iterators membership testing

2015-08-09 Thread Laura Creighton
Maybe add something about this here? https://docs.python.org/2/tutorial/classes.html#iterators Laura -- https://mail.python.org/mailman/listinfo/python-list

Re: Iterators membership testing

2015-08-09 Thread Chris Angelico
On Sun, Aug 9, 2015 at 9:00 PM, Pierre Quentel wrote: >> The trap you're seeing here is that iterating over an iterator always >> consumes it, but mentally, you're expecting this to be iterating over >> a new instance of the same sequence. > > No, I just tried to apply what I read in the docs : >

Re: Iterators membership testing

2015-08-09 Thread Pierre Quentel
> The trap you're seeing here is that iterating over an iterator always > consumes it, but mentally, you're expecting this to be iterating over > a new instance of the same sequence. No, I just tried to apply what I read in the docs : 1. I have y = A(10) which is an instance of a class which doe

Re: Iterators membership testing

2015-08-09 Thread Chris Angelico
On Sun, Aug 9, 2015 at 7:55 PM, Pierre Quentel wrote: > Thanks for the explanation. I understand that an iterator can't test > membership any other way, but I'm still worried about how the documentation > explains it. Reading it, I naively expected that an iterator which produces > the integer

Re: Iterators membership testing

2015-08-09 Thread Pierre Quentel
Le dimanche 9 août 2015 11:25:17 UTC+2, Chris Angelico a écrit : > On Sun, Aug 9, 2015 at 7:06 PM, Pierre Quentel > wrote: > > "For user-defined classes which do not define __contains__() but do define > > __iter__(), x in y is true if some value z with x == z is produced while > > iterating over

Re: Iterators membership testing

2015-08-09 Thread Chris Angelico
On Sun, Aug 9, 2015 at 7:06 PM, Pierre Quentel wrote: > "For user-defined classes which do not define __contains__() but do define > __iter__(), x in y is true if some value z with x == z is produced while > iterating over y. If an exception is raised during the iteration, it is as if > in raised