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


Groups > comp.lang.python > #110772

Re: Language improvement: Get more from the `for .. else` clause

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Victor Savu <victor.nicolae.savu@gmail.com>
Newsgroups comp.lang.python
Subject Re: Language improvement: Get more from the `for .. else` clause
Date Wed, 29 Jun 2016 13:11:20 +0200
Lines 50
Message-ID <mailman.104.1467198683.2358.python-list@python.org> (permalink)
References <CAGtOLTeZAkPA-CUehRkaj-7hUCStSSkEzaVH9JQfQfU1yO9Ewg@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de OWkm8MExdQNolh7ypOeougJC1G15Sh4tKiaFgVOC+2iA==
Return-Path <victor.nicolae.savu@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.044
X-Spam-Evidence '*H*': 0.91; '*S*': 0.00; 'value,': 0.03; 'cc:addr :python-list': 0.09; 'def': 0.13; '2016': 0.16; 'bars,': 0.16; 'example?': 0.16; 'generator.': 0.16; 'logging,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:Language': 0.16; 'sure.': 0.16; 'wrote:': 0.16; 'email addr:gmail.com&gt;': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'proposed': 0.20; 'cc:no real name:2**0': 0.22; 'feature': 0.24; 'mon,': 0.24; 'message-id:@mail.gmail.com': 0.27; 'yield': 0.27; 'values': 0.28; "i'm": 0.30; 'skip:[ 10': 0.31; 'returned': 0.32; 'statement': 0.32; 'michael': 0.33; 'skip:d 20': 0.34; 'received:google.com': 0.35; 'best,': 0.35; 'trouble': 0.35; 'necessary.': 0.35; 'but': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'progress': 0.38; 'application': 0.39; 'subject:from': 0.39; 'subject:the': 0.39; 'subject:more': 0.61; 'show': 0.62; 'more': 0.63; 'capture': 0.66; 'subject:Get': 0.66; 'skip:\xc2 10': 0.67; '8bit%:100': 0.70; 'transfer': 0.73; 'decorate': 0.84; 'subject:else': 0.84; 'victor': 0.84; 'yielded': 0.84; 'realistic': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to:cc; bh=7qYlsZTrpNoBsrif05SBuwQcwpqX9TSk4tHlnyJW+Tc=; b=MYGFXSuwRL82JIlrUqWx8+WL0jPUMBofxw+81gQChMcZbCooBTosfItuObQY+GP9hZ fYG0yeVdKXmWSZLrZO2YvaImxqkkBTdS+f5zb/71NJRPrZ7S1hWPPH+Yq6dK+Tsc9gfh NuH3h1nHoeQ9+jrgOts0P5iTdK612GLrwl8zQ/GQVrF43IM2Zy/glsxTNWIMGsrf3aZ1 gkC+gFArV0FqQBQSpL8tYZ9/pGDFqAm6bXnAU/DXh7SRfDJwNCJU5mFr+Jtm4LMR/rDv R4IgnmBQxZq+P2h62teoPALe4DzG+8DhpQUxn46ggFF/Rfrl+P0aMcTbGA+J4EWjoKFz x7pQ==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:from:date:message-id:subject:to:cc; bh=7qYlsZTrpNoBsrif05SBuwQcwpqX9TSk4tHlnyJW+Tc=; b=FCpH67BNOY/1T4YcSVbicg1l8xj9dYnL43tLLS4tyf/q89ckdoTTdOSgp6HCnoQN3t EpQYmKDqGQe3Uspa/P9ycM11ySzHmpjfpwn5MSQAIPap+T1v33fL4YeoKefaMc1bPOXO 40QRrcwEh/5nNgx2Bm5n29U+AJGLcQVOQeydioLbzBAXU/B+L5W5tdD63JD//C6U9uSx RdnnOKdW5yWa2FEe8rPP+8QvaPtNzM5CRCj9LebCQJTnmJX7TJMC3iZcK3guXfhHBbqd DYmuNjcRqazk9cwXOvUeMKYzncj6baw0UWLf8n/j5niBIQPcKtO9Dh0i8wEcXMiUe/m/ EWuw==
X-Gm-Message-State ALyK8tKEzE917gfQ/corok+Zg/XR6ep8XWyBRVrAuzlKE4MZTmRHrZai8G1bNSnyMv9hOT5aBOa4PIX0VS1JKA==
X-Received by 10.28.11.129 with SMTP id 123mr21676002wml.84.1467198681192; Wed, 29 Jun 2016 04:11:21 -0700 (PDT)
X-Content-Filtered-By Mailman/MimeDel 2.1.22
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.22
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>
X-Mailman-Original-Message-ID <CAGtOLTeZAkPA-CUehRkaj-7hUCStSSkEzaVH9JQfQfU1yO9Ewg@mail.gmail.com>
Xref csiph.com comp.lang.python:110772

Show key headers only | View raw


Sure.

Simple use-case: Decorate the yielded values and the return value of a
generator. Right now, with `yield from` you can only decorate the return
value, whereas with a for loop you can decorate the yielded values, but you
sacrifice the returned value altogether.

```
def ret_decorator(target_generator):
    returned_value = yield from target_generator()
    return decorate_ret(returned_value)

def yield_decorator(target_generator):
    for yielded_value in target_generator:
        yield decorate_yield(yielded_value)

    # Nothing to return. the target return value was
    # consumed by the for loop
```

With the proposed syntax, you can decorate both:

```
def decorator(target_generator):
    for yielded_value in target_generator:
        yield decorate_yield(yielded_value)
    else returned_value:
        return decorate_ret(returned_value)
```

Please let me know if you are interested in a more concrete case such as a
domain-specific application (I can think of progress bars, logging,
transfer rate statistics ...).

Best,
VS

On Mon, Jun 27, 2016 at 5:06 PM, Michael Selik <michael.selik@gmail.com>
wrote:

> On Mon, Jun 27, 2016 at 12:53 AM Victor Savu <
> victor.nicolae.savu@gmail.com> wrote:
>
>> capture the [StopIteration] value in the `else` statement of the `for`
>> loop
>>
>
> I'm having trouble thinking of a case when this new feature is necessary.
> Can you show a more realistic example?
>

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


Thread

Re: Language improvement: Get more from the `for .. else` clause Victor Savu <victor.nicolae.savu@gmail.com> - 2016-06-29 13:11 +0200

csiph-web