Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

A list is only as strong as its weakest link. -- Don Knuth


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.165.1646416071.2329.python-list@python.org>

  copy mid

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

  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: Sat, 5 Mar 2022 04:47:37 +1100
Lines: 56
Message-ID: <mailman.165.1646416071.2329.python-list@python.org>
References: <CALq4Z0-fJk-HOu0ka2kPrOioPYAh3e3zbziwetUDmAAx1U1LMw@mail.gmail.com>
<YiFCPlGC+2aRIR0K@shallowsky.com>
<21739669.459456.1646348879560@mail.yahoo.com>
<e3544242-cd54-05d3-2101-5b9fedc1c13e@btinternet.com>
<25121.48093.26762.592197@ixdm.fritz.box>
<1277767760.379444.1646412189311@mail.yahoo.com>
<CAPTjJmpGph9woqQGnkoE9=auFw49UqYMr3OxbhK24LytUgdyEA@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de 1HAp/2H+t+bxk/rTNWO8/Qqd2JZ5/dx4paCgNbFX4ZlQ==
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=EqT5EgEU;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.003
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '(for': 0.05; '2022': 0.05;
'searching': 0.05; 'variable': 0.05; 'lets': 0.07; 'loop': 0.07;
'mar': 0.07; 'modification': 0.07; 'else:': 0.09; 'something,':
0.09; 'avi': 0.16; 'chrisa': 0.16; 'found,': 0.16;
'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16;
'gross': 0.16; 'part.': 0.16; 'place?': 0.16;
'received:209.85.221.42': 0.16; 'received:mail-
wr1-f42.google.com': 0.16; 'subject:else': 0.16; 'wrote:': 0.16;
'to:addr:python-list': 0.20; 'sat,': 0.22; 'code': 0.23; 'to:name
:python-list@python.org': 0.24; 'depends': 0.25; 'else': 0.27;
'sense': 0.28; 'suggest': 0.28; 'it,': 0.29; "doesn't": 0.32;
'empty': 0.32; 'python-list': 0.32; 'message-id:@mail.gmail.com':
0.32; 'unless': 0.32; 'but': 0.32; 'subject:for': 0.33; 'there':
0.33; 'same': 0.34; 'header:In-Reply-To:1': 0.34;
'received:google.com': 0.34; 'english,': 0.35; 'item': 0.35;
'yes,': 0.35; 'from:addr:gmail.com': 0.35; 'using': 0.37;
'received:209.85': 0.37; 'others': 0.37; 'way': 0.38; 'could':
0.38; 'received:209': 0.39; 'use': 0.39; 'break': 0.39; 'finding':
0.39; 'rest': 0.39; 'still': 0.40; 'otherwise,': 0.40;
'something': 0.40; 'search': 0.61; 'above': 0.62; 'here': 0.62;
'clear': 0.64; 'your': 0.64; 'similar': 0.65; 'look': 0.65;
'advantages': 0.69; "you'll": 0.73; 'clause': 0.84; 'integral':
0.84; 'thing?': 0.84; 'retain': 0.91; 'green': 0.96
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
:content-transfer-encoding;
bh=/PWvlEC0G/MUMS+HIhrfhj2GgBNNP6QAK1wedbFQuXQ=;
b=EqT5EgEUB8OaWe1lEzinK6gwyGAHyuaAlh0tmQFo+0QFvuSmIzFw43MewJCD3A1DDQ
lXy2BKvgMfJG1It/lhRV3daJk0kqCfNfqbbrEKZazDD2js+oh/QlDSbfqVJjEOyeo+nJ
BartZpj8+ffVdb+894IDcpW+6DfSA42UqGnH+tM6Pzt/Z5YUfQu+Cy6oaeYqKYQqf6kd
gO0Kv3/wqrzEK1TYK+eKLOjbmjGNfp5FetkdzLgNPtQztOob0fH2gqweanUAsShJYrGc
flRRblXXrW32J1PRxteRH3rvPi1rrbmLurIhOoeyUVz3XbIXoQvzPZCwKYl3qy7xJ7at
EY7g==
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:content-transfer-encoding;
bh=/PWvlEC0G/MUMS+HIhrfhj2GgBNNP6QAK1wedbFQuXQ=;
b=fXhSrdepsefpAnTdBC5OCVdo1nSFnXBUWAG3lTjdNB9OXugFUNdv75u+kUXVzj9B1y
2wlU2eVlGj7+4sdQlSE2pdnupiaTXLdcP4fcESNFkCWxvmGb2HgP0mEEXABfnjfWaYoV
8rT7Rh+R/U5c2cneSrDwgs7WwACc+2Dn52h4qq7Bk1yyvq3mF180bOu/0BbKuPqH5FQL
jPBgLpq+97y6fypOuBDRS4er+mElCGwFKTrmDQaMobyXFiCfbfu4AS3mVK9G6Xt2t5BZ
CMez6f2gM3nqe5iGQX8E0S3838C/IFMDPJE+Uuf6h2ZlOnI2n9Gsvue2wuGYNE3Kaok9
esTg==
X-Gm-Message-State: AOAM533yBvaTA/wJVvwkhtH936lfDIYIQIAmiaIRrPR35vZewCvYzHoU
nUnpdXfynsaRpxbJ8eNwsMz3nimqnczy3X0rKlJNByFX
X-Google-Smtp-Source: ABdhPJxgrHn3nxr4eU+7dY3q/NkrPmx8wv4JBIIp6O+nkQmDAjO2EJp8fRbWbcLrlYIuuu/hBRZ75seuFxPBQT8Os5s=
X-Received: by 2002:adf:f801:0:b0:1f0:7675:f5e6 with SMTP id
s1-20020adff801000000b001f07675f5e6mr3190027wrp.564.1646416069371; Fri, 04
Mar 2022 09:47:49 -0800 (PST)
In-Reply-To: <1277767760.379444.1646412189311@mail.yahoo.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: <CAPTjJmpGph9woqQGnkoE9=auFw49UqYMr3OxbhK24LytUgdyEA@mail.gmail.com>
X-Mailman-Original-References: <CALq4Z0-fJk-HOu0ka2kPrOioPYAh3e3zbziwetUDmAAx1U1LMw@mail.gmail.com>
<YiFCPlGC+2aRIR0K@shallowsky.com>
<21739669.459456.1646348879560@mail.yahoo.com>
<e3544242-cd54-05d3-2101-5b9fedc1c13e@btinternet.com>
<25121.48093.26762.592197@ixdm.fritz.box>
<1277767760.379444.1646412189311@mail.yahoo.com>
 by: Chris Angelico - Fri, 4 Mar 2022 17:47 UTC

On Sat, 5 Mar 2022 at 03:44, Avi Gross via Python-list
<python-list@python.org> wrote:
>
> Dieter,
>
> Your use is creative albeit it is not "needed" since all it does is make sure your variable is initialized to something, specifically None.
>
> So would this not do the same thing?
>
> eye = None
>
> for eye in range(0):
> print(eye)
>
> eye
>
> If I understand it, your example depends on a variable that is not yet initialized to be used in a loop and retain the last value after the loop. You then set it to None if it is not used as the loop is skipped. Others have shown an example similar to the above of using a sentinel that lets you know if the loop is skipped.
>
> Of course, there are some advantages in making it clear by doing it you way that the loop (for example if copied and used elsewhere) needs to include the else statement as an integral part.
>
> I would like to suggest a slight modification to the above as in if you are searching for something in either seq1 and if not found in seq2. Call it looking for your green shirt in the closet and if not found, looking in the attic. Would this code make sense as such a use in several ways? In English, look here first and if there is NOTHING there, look in the second place?
>
> closet = []
>
> attic = ["Costumes", "Sheets", "Shirts" ]
>
> for item in closet:
> print(item)
> if item == "Shirts" : print("FOUND in closet!!")
> else:
> for item in attic:
> print(item)
> if item == "Shirts" : print("FOUND in attic!!")
>
> Yes, as discussed, you could do an IF statement to check if closet is empty but for iterators, it gets ...
>

Make sure you 'break' after finding it. Otherwise, you'll keep
searching the rest of your closet, and then still go on to search your
attic. The "else:" clause doesn't help you here unless that break is
present.

ChrisA

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor