Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: avoid for loop calling Generator function Date: Tue, 23 Feb 2016 02:46:33 +1100 Lines: 14 Message-ID: References: <0c289f93-ce64-4eff-93f3-c70d7ff50817@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de xT21I8Q3fSMZHF8ENSd15AMglhdeXPKuvEzKwqwfAiAw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'none,': 0.05; 'none)': 0.07; 'ugly': 0.07; 'cc:addr:python-list': 0.09; 'here?': 0.09; '2016': 0.16; '23,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'hack,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'parameter': 0.22; 'am,': 0.23; 'feb': 0.23; 'second': 0.24; 'thus': 0.24; 'header:In-Reply-To:1': 0.24; 'message-id:@mail.gmail.com': 0.27; 'print': 0.30; 'maybe': 0.33; 'raising': 0.33; 'tue,': 0.34; 'received:google.com': 0.35; 'filter': 0.35; 'according': 0.36; 'instead': 0.36; 'received:209.85': 0.36; 'subject:: ': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'why': 0.39; 'entire': 0.61; 'skip:n 10': 0.62; 'beautiful': 0.66; 'chrisa': 0.84; 'to:none': 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; bh=n/+WNXo7c726BzhnUpzo1QdClrSgwpWOUDraOB+2kmo=; b=Nr6ZSuNBb1oT1VPtHGCh7E9DV/LICgjfRga+gB6ieERPioG9NWKj6iX7t1/KOWXIj7 BSuF0a+5Yj7a1aGQcdSFn0ZoDge14v4OlrooikKyFQ2YXpJr4xLQx+3KihodF0tgkKk0 U2Bou/kSmdbTwK2Rl5m46CawTq5rn+KXZtXwiL7Qxzk4HQ2lbcjoNCM4QiI91RoGLDXr AnoOBHA8oVJ6hUkWkt/c1TA/GDEqegjw07jOsw9SEvKUVJKz0O5MDDzAr5qtdUqeV9/d yx+uV7tPm8O3f0N5RM0LWOkvNhct120OV4YvKhUeneG/ecdmGXaZX7ggZS+HjB5lhlAc ysoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc; bh=n/+WNXo7c726BzhnUpzo1QdClrSgwpWOUDraOB+2kmo=; b=RybAepSkBygPhKEIFICxstTvfIVnbO8ZfdXpU/0PH7WdHs6KJ2U7p8ElPbULsYta2d Od10U9DHTg/keLb5t0+iHKhok1C5ZCl7//gFS43P1VqGq8cCRX2XYdQ4ILuS9COrdC2z SmZWx4k+N9e74KQLZzYL7RvyLr5BSCLH+9Kyf3jaksmat87gtIB6r6AGMNQ9IInE6wTq ImPRQrT0AfKcR8pA73iS+/h2tyT3lq7iBhGPePlgnr8GoHTQgs2C8/xIwfdyPrv/b7Zh UC5MRLKdz25HvaV8vPwUbb4fcn856Zg+uSJAYivOrYywLvSLSlnMYAA1dj8KdWzAqDSl etEA== X-Gm-Message-State: AG10YOTJimXmVB2KkfexxZ2XRwRc3oM1A9T27RJVZNSdIpn2OS55soZWc3TRPyQ8MX//2KgMyXWPORVNuX/2aA== X-Received: by 10.50.28.105 with SMTP id a9mr12636434igh.94.1456155993884; Mon, 22 Feb 2016 07:46:33 -0800 (PST) In-Reply-To: <0c289f93-ce64-4eff-93f3-c70d7ff50817@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21rc2 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:103344 On Tue, Feb 23, 2016 at 2:38 AM, Arshpreet Singh 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