Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #95182
| Path | csiph.com!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <rosuav@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'value,': 0.03; 'testing,': 0.05; 'false,': 0.07; 'objects,': 0.07; 'val': 0.07; 'cc:addr:python-list': 0.09; "'%s": 0.09; 'way:': 0.09; 'exception': 0.13; 'def': 0.13; 'interpreter': 0.15; 'duplicating': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iterables': 0.16; 'iterating': 0.16; 'iteration': 0.16; 'iteration,': 0.16; 'iterator': 0.16; 'iterator,': 0.16; 'iterator:': 0.16; 'iterators': 0.16; 'user- defined': 0.16; 'value:': 0.16; 'wrote:': 0.16; 'test.': 0.18; '>>>': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'aug': 0.20; '"",': 0.22; 'resumes': 0.22; 'trace': 0.22; 'pass': 0.22; '(or': 0.23; 'this:': 0.23; '(most': 0.24; 'header :In-Reply-To:1': 0.24; 'point.': 0.27; 'define': 0.27; 'right.': 0.27; 'message-id:@mail.gmail.com': 0.27; 'yield': 0.27; 'values': 0.28; 'classes': 0.30; 'work.': 0.30; 'error.': 0.31; 'skip:_ 10': 0.32; 'raised': 0.33; 'traceback': 0.33; 'values.': 0.33; 'file': 0.34; 'membership': 0.34; 'handle': 0.34; 'lists': 0.34; 'list': 0.34; 'received:google.com': 0.35; 'false': 0.35; 'something': 0.35; 'but': 0.36; 'skip:i 20': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'setting': 0.37; "won't": 0.38; 'test': 0.39; 'does': 0.39; 'where': 0.40; 'some': 0.40; 'your': 0.60; 'different': 0.63; 'times': 0.63; "they're": 0.66; 'chrisa': 0.84; 'yielded': 0.84; 'to:none': 0.91; 'more:': 0.91; 'subject:membership': 0.91 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=u+WUrUFCrr7/CsDN7F6MMpXKG2emFD/gJ53Nn78VaKk=; b=JcToVokRYx8/vvhzT33WBmovaLaCoVzEZ013OjzFP1t869JfoQEQ1n8nM4S/vg3cLm xBD+48i8n42m/DfRnkw57BwAluGpFRH/rXmiIv8Aois/sGd4CG66SMVV7zlzOiTwqrvR oHgpOtqT3JjZ9efNSWd2SYguPhf2DXJf8D2iHb6hc+uAYleyqKcD3pYf5ukk8PPjcorg 3ZFGwJg2hf3mrgZToaV34P0AtbCGmjLx0gLy4HwOIhTUjOF1R67geUl+u7zoMALO5WQd nIfzVHLloizsgQjhPnsemK2QRfL1rI8i/a1TdwCQ6ogcsThPHyTGqhmREJmFfj+JxPn2 vMGw== |
| MIME-Version | 1.0 |
| X-Received | by 10.50.124.97 with SMTP id mh1mr6396890igb.92.1439112278699; Sun, 09 Aug 2015 02:24:38 -0700 (PDT) |
| In-Reply-To | <88256581-75d4-4f77-81f0-9e3e25baecbc@googlegroups.com> |
| References | <88256581-75d4-4f77-81f0-9e3e25baecbc@googlegroups.com> |
| Date | Sun, 9 Aug 2015 19:24:38 +1000 |
| Subject | Re: Iterators membership testing |
| From | Chris Angelico <rosuav@gmail.com> |
| Cc | "python-list@python.org" <python-list@python.org> |
| Content-Type | text/plain; charset=UTF-8 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.20+ |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2.1439112286.3627.python-list@python.org> (permalink) |
| Lines | 76 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1439112286 news.xs4all.nl 2887 [2001:888:2000:d::a6]:34846 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:95182 |
Show key headers only | View raw
On Sun, Aug 9, 2015 at 7:06 PM, Pierre Quentel <pierre.quentel@gmail.com> 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 that exception."
>
> ...
> I get an assertion error. Setting a trace on __next__ suggests that for
> membership testing, the interpreter consumes the iterator until the searched
> value is found (or until exhaustion), then it resumes iteration at this point.
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
That's what the docs describe. So what you have is something like this:
for i in iterator:
for j in iterator:
if i == j: break
else:
assert False, '%s not found' %i
You're dragging values from the same iterator, so you're consuming it
as part of your membership test. You can do this kind of thing:
>>> 5 in A(10)
True
but if you've already consumed a few values, those won't be in the
iterator any more:
>>> x = A(10)
>>> next(x)
0
>>> next(x)
1
>>> next(x)
2
>>> next(x)
3
>>> 2 in x
False
This is simply how iterators work. They're very different from
repeatable iterables like lists or range objects, where you _can_ test
for membership that way:
>>> x = [10,20,30]
>>> for i in x: assert i in x
...
>>> x = iter([10,20,30])
>>> for i in x: assert i in x
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
Note that I _could_ create a list that would pass this assertion,
simply by duplicating every value:
>>> x = iter([10,10,20,20,30,30])
>>> for i in x: assert i in x
...
But it's iterating only three times here, and the 'in' check is
consuming the other three values. Once your A(10) has yielded some
value, it will never yield it again, so the assertion can never pass.
Does that explain matters?
ChrisA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Iterators membership testing Pierre Quentel <pierre.quentel@gmail.com> - 2015-08-09 02:06 -0700
Re: Iterators membership testing Chris Angelico <rosuav@gmail.com> - 2015-08-09 19:24 +1000
Re: Iterators membership testing Pierre Quentel <pierre.quentel@gmail.com> - 2015-08-09 02:55 -0700
Re: Iterators membership testing Chris Angelico <rosuav@gmail.com> - 2015-08-09 20:13 +1000
Re: Iterators membership testing Pierre Quentel <pierre.quentel@gmail.com> - 2015-08-09 04:00 -0700
Re: Iterators membership testing Chris Angelico <rosuav@gmail.com> - 2015-08-09 21:10 +1000
Re: Iterators membership testing Laura Creighton <lac@openend.se> - 2015-08-09 13:30 +0200
Re: Iterators membership testing Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-08-09 14:49 +0100
Re: Iterators membership testing Chris Angelico <rosuav@gmail.com> - 2015-08-09 23:11 +1000
Re: Iterators membership testing Tim Chase <python.list@tim.thechases.com> - 2015-08-09 08:09 -0500
Re: Iterators membership testing Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-08-09 14:45 +0100
Re: Iterators membership testing Tim Chase <tim@thechases.com> - 2015-08-09 08:09 -0500
csiph-web