Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

There are no games on this system.


devel / comp.lang.python / Re: Behavior of the for-else construct

SubjectAuthor
o Re: Behavior of the for-else constructChris Angelico

1
Re: Behavior of the for-else construct

<mailman.119.1646319750.2329.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17296&group=comp.lang.python#17296

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ros...@gmail.com (Chris Angelico)
Newsgroups: comp.lang.python
Subject: Re: Behavior of the for-else construct
Date: Fri, 4 Mar 2022 02:02:17 +1100
Lines: 61
Message-ID: <mailman.119.1646319750.2329.python-list@python.org>
References: <CALq4Z0-fJk-HOu0ka2kPrOioPYAh3e3zbziwetUDmAAx1U1LMw@mail.gmail.com>
<CAPTjJmr1KNiEtw9AB5382jZREuT=oDK62ABZ6ie1xA8r9sWS+Q@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de hOvB06SBT1ANqsMYtRx97wIludJzD/TTpsjovot+oicg==
Return-Path: <rosuav@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=FhkY/upu;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.001
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python?': 0.03; '(which':
0.04; '2022': 0.05; 'parallel': 0.05; 'variable': 0.05; 'loop':
0.07; 'mar': 0.07; 'construct': 0.09; 'else:': 0.09; 'proposing':
0.09; 'break,': 0.16; 'chrisa': 0.16; 'confused': 0.16;
'executed': 0.16; 'flag': 0.16; 'from:addr:rosuav': 0.16;
'from:name:chris angelico': 0.16; 'iterable': 0.16; 'iteration':
0.16; 'on).': 0.16; 'once.': 0.16; 'sensible': 0.16; 'separately':
0.16; 'separately,': 0.16; 'subject:else': 0.16; 'universe,':
0.16; 'useful.': 0.16; 'wrote:': 0.16; "can't": 0.17; "aren't":
0.19; 'to:addr:python-list': 0.20; 'issue': 0.21; 'exception':
0.22; 'fri,': 0.22; 'maybe': 0.22; 'so.': 0.26; 'else': 0.27;
'greatly': 0.28; 'goes': 0.28; 'whole': 0.30; 'think': 0.32;
"doesn't": 0.32; 'elements': 0.32; 'empty': 0.32; 'retrieve':
0.32; "wouldn't": 0.32; 'message-id:@mail.gmail.com': 0.32; 'but':
0.32; "i'm": 0.33; 'subject:for': 0.33; 'there': 0.33; 'someone':
0.34; 'header:In-Reply-To:1': 0.34; 'received:google.com': 0.34;
'trying': 0.35; 'yes,': 0.35; 'close': 0.35;
'from:addr:gmail.com': 0.35; 'also,': 0.36; 'people': 0.36;
'really': 0.37; 'using': 0.37; "it's": 0.37; 'received:209.85':
0.37; 'could': 0.38; 'means': 0.38; 'received:209': 0.39; 'block':
0.39; 'much.': 0.39; 'forms': 0.40; 'both': 0.40; 'want': 0.40;
'try': 0.40; 'sorry': 0.60; 'here.': 0.61; 'finally': 0.62;
'once': 0.63; 'imagine': 0.64; 'your': 0.64; 'now,': 0.67;
'instead,': 0.70; 'survey': 0.71; 'little': 0.73; 'easy': 0.74;
'ship': 0.81; 'behavior:': 0.84; 'body.': 0.91; 'broke': 0.91
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
h=mime-version:references:in-reply-to:from:date:message-id:subject:to;
bh=D91Uw6Rk/tpO2BaELS0pSZYnXswdIiVcGR8FbHPWRco=;
b=FhkY/upu3sG0rKlv8ojoowRB20Ex2c2DCgpvz58C8eXHyBpApT1QUKb3damGnhZUg5
8suOjmM6DEuEZBGOVfJVY3PBc33GJTa2cRG3lre5VpjqvRxmj8kGnXhsb0i5pXaN49Sj
bKnBEYkMt6+zs9MLpJfiQn7VZkqO53gW5XKeyzq65ZV5YZlOq7gSowO3uEhGIHJvIY9N
TzzCaWvZQwuFNVMo6oD1h9aqFO2oODBAwSOB+TPEQqDEonanEPOBGPSMOJC0VAf8pTqu
uLpUBi2kuEYotY7LNl/8gah2DNh+eXtbh58C71Xg1RRpGQtHrc9zfKH7KMcK3r9Viyl0
f1VQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20210112;
h=x-gm-message-state:mime-version:references:in-reply-to:from:date
:message-id:subject:to;
bh=D91Uw6Rk/tpO2BaELS0pSZYnXswdIiVcGR8FbHPWRco=;
b=lDHoO7NH+tFs8wKVUemS0BcpsUf+Udl4Eu7L8RK92IdXZ8xIZIo86OVN311DrIFOct
5A0FsxMCKoh0YTzlciBF+5MUZLOc5q8XC7iZbwMHi54MwOxD/taihl0Nm2y2Lv3HgC6N
5nP5IA6g+PbpOU8+M85vRaDQnFPckeX3jeecHP0fPNai4JjR696Q8FRK5yK2gpjZowVa
4H3qNyLbADnE67N9NajQH+STm9KwstHdDlMEtv3UKM+nSybePxErW6ssip1h1MPL5ARb
9bcbD8X2qjSBFyW5kteajlIwemC69r22elNLJXPF/t1oBE0ZP18EbpcuYOFXJGXu8Ex8
F7rQ==
X-Gm-Message-State: AOAM5320GP5MCTgZRz8XYH2skyVK6vL7TlXWXRJWyjr2OCpGOx9CNZC7
QaSENi8jYxCbmDHIxvMv5kGjfrhbYl5EBEcpi8gsyed7
X-Google-Smtp-Source: ABdhPJzvulzM5TuzRt+c0JvekCnDMab1Uck0l/hIyUCLQjXuiORnixI4NIWkLIgKFJp6pWj5RLzOy90SH2RmWj6+Tvs=
X-Received: by 2002:a05:600c:4151:b0:381:4bed:a355 with SMTP id
h17-20020a05600c415100b003814beda355mr4163461wmm.135.1646319748559; Thu, 03
Mar 2022 07:02:28 -0800 (PST)
In-Reply-To: <CALq4Z0-fJk-HOu0ka2kPrOioPYAh3e3zbziwetUDmAAx1U1LMw@mail.gmail.com>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
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: <https://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: <CAPTjJmr1KNiEtw9AB5382jZREuT=oDK62ABZ6ie1xA8r9sWS+Q@mail.gmail.com>
X-Mailman-Original-References: <CALq4Z0-fJk-HOu0ka2kPrOioPYAh3e3zbziwetUDmAAx1U1LMw@mail.gmail.com>
 by: Chris Angelico - Thu, 3 Mar 2022 15:02 UTC

On Fri, 4 Mar 2022 at 00:25, computermaster360
<computermaster360@gmail.com> wrote:
>
> I want to make a little survey here.
>
> Do you find the for-else construct useful? Have you used it in
> practice? Do you even know how it works, or that there is such a thing
> in Python?

Yes, yes, and yes-yes. It's extremely useful.

> I have used it maybe once. My issue with this construct is that
> calling the second block `else` doesn't make sense; a much more
> sensible name would be `then`.

Ehh, I think "finally" would be a better keyword, but that has very
close connections with exception handling.

> Now, imagine a parallel universe, where the for-else construct would
> have a different behavior:
>
> for elem in iterable:
> process(elem)
> else:
> # executed only when the iterable was initially empty
> print('Nothing to process')
>
> Wouldn't this be more natural? I think so. Also, I face this case much
> more often than having detect whether I broke out of a loop early
> (which is what the current for-else construct is for).

This also has value, but not as much.

> Now someone may argue that it's easy to check whether the iterable is
> empty beforehand. But is it really? What if it's an iterator?
> Then one would have to resort to using a flag variable and set it in
> each iteration of the loop. An ugly alternative would be trying to
> retrieve
> the first element of the iterable separately, in a try block before
> the for-loop, to find out whether the iterable is empty. This would of
> course
> require making an iterator of the iterable first (since we can't be
> sure it is already an iterator), and then -- if there are any elements
> -- processing
> the first element separately before the for-loop, which means
> duplicating the loop body. You can see the whole thing gets really
> ugly really quickly...
>
> What are your thoughts? Do you agree? Or am I just not Dutch enough...?

Both forms have value, and only one of them can be called for-else.
Maybe if the current one had been called for-finally, then what you're
proposing could have been for-else, and we could theoretically have
had for-else-finally (where it goes into the for block once for each
element, but if there aren't any, it goes into the else block instead,
and either way, if you never break, it goes into the finally before
moving on). That ship has sailed, though, and given that people would
be confused very greatly by for-finally, I'm not overly sorry with the
current state of affairs.

ChrisA

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor