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


Groups > comp.lang.python > #103344

Re: avoid for loop calling Generator function

From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: avoid for loop calling Generator function
Date 2016-02-23 02:46 +1100
Message-ID <mailman.42.1456156002.20994.python-list@python.org> (permalink)
References <e5e4a934-4eeb-46ed-892f-cda9e903c1cd@googlegroups.com> <mailman.40.1456148110.20994.python-list@python.org> <0c289f93-ce64-4eff-93f3-c70d7ff50817@googlegroups.com>

Show all headers | View raw


On Tue, Feb 23, 2016 at 2:38 AM, Arshpreet Singh <arsh840@gmail.com> wrote:
>> next(filter(print, read_pdf("book.pdf")), None)
>
> Why we are w=using filter here?

It's a beautiful hack. It'll filter according to the "print"
predicate, which always returns None, and will thus filter everything
out. One single call to next() will thus process and print the entire
PDF, and then - since it has a second parameter - return None instead
of raising StopIteration.

Or maybe it's a gross and ugly hack, with the exact same description.

ChrisA

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


Thread

avoid for loop calling Generator function Arshpreet Singh <arsh840@gmail.com> - 2016-02-22 03:15 -0800
  Re: avoid for loop calling Generator function Peter Otten <__peter__@web.de> - 2016-02-22 14:34 +0100
    Re: avoid for loop calling Generator function Arshpreet Singh <arsh840@gmail.com> - 2016-02-22 07:38 -0800
      Re: avoid for loop calling Generator function Chris Angelico <rosuav@gmail.com> - 2016-02-23 02:46 +1100
      Re: avoid for loop calling Generator function Ian Kelly <ian.g.kelly@gmail.com> - 2016-02-22 09:11 -0700

csiph-web