Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #95189

Re: Iterators membership testing

Date 2015-08-09 08:09 -0500
From Tim Chase <python.list@tim.thechases.com>
Subject Re: Iterators membership testing
References <88256581-75d4-4f77-81f0-9e3e25baecbc@googlegroups.com> <CAPTjJmoibrvoxFt7o3XA+h_5ETTCS1b9uNUOhK=1UpRvo3PTpQ@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.7.1439126856.3627.python-list@python.org> (permalink)

Show all headers | View raw


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

  any(i == obj for obj in iter)

The addition of any/all initially struck me as a "why?! this is so
easy to write in-line" moment, only to find myself using them all()
the time. :-) The code-intention becomes so much clearer.  Even
back-ported them to 2.4 code that I maintain.

-tkc


Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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