Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

A computer scientist is someone who fixes things that aren't broken.


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

SubjectAuthor
o Re: Behavior of the for-else constructAvi Gross

1
Re: Behavior of the for-else construct

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: avigr...@verizon.net (Avi Gross)
Newsgroups: comp.lang.python
Subject: Re: Behavior of the for-else construct
Date: Fri, 4 Mar 2022 20:52:43 +0000 (UTC)
Lines: 190
Message-ID: <mailman.170.1646427179.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>
<1957563644.11129.1646427163819@mail.yahoo.com>
Reply-To: Avi Gross <avigross@verizon.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de C79scgIm1F3aH4Lmsyjy/gcqucVx6EDmEDSrMfyF6nhQ==
Return-Path: <avigross@verizon.net>
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=verizon.net header.i=@verizon.net header.b=Aoy7wZKs;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.003
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'looks': 0.02; '(for':
0.05; '2022': 0.05; 'run.': 0.05; 'searching': 0.05; 'variable':
0.05; 'lets': 0.07; 'loop': 0.07; 'mar': 0.07; 'modification':
0.07; 'tests': 0.07; 'angelico': 0.09; 'construct': 0.09; 'else:':
0.09; 'fails': 0.09; 'meant': 0.09; 'something,': 0.09;
'url:mailman': 0.15; 'avi': 0.16; 'chrisa': 0.16; 'for.': 0.16;
'found,': 0.16; 'functions,': 0.16; 'general.': 0.16; 'gross':
0.16; 'idea.': 0.16; 'loops': 0.16; 'nested': 0.16; 'none:': 0.16;
'part.': 0.16; 'place?': 0.16; 'print(x)': 0.16;
'received:74.6.131': 0.16; 'sequentially': 0.16; 'subject:else':
0.16; 'touched': 0.16; 'variable,': 0.16; 'variable.': 0.16;
'wrote:': 0.16; 'calls': 0.19; 'to:addr:python-list': 0.20;
'issue': 0.21; 'exception': 0.22; 'fri,': 0.22; 'ran': 0.22;
'sat,': 0.22; 'code': 0.23; 'run': 0.23; 'to:name:python-
list@python.org': 0.24; 'skip:- 10': 0.25; 'url-
ip:188.166.95.178/32': 0.25; 'url-ip:188.166.95/24': 0.25;
'depends': 0.25; 'url:listinfo': 0.25; 'url-ip:188.166/16': 0.25;
'behavior': 0.26; 'normally': 0.26; 'leave': 0.27; 'else': 0.27;
'local': 0.27; 'bit': 0.27; 'function': 0.27; 'done': 0.28;
'chris': 0.28; 'expect': 0.28; 'sense': 0.28; 'series': 0.28;
'suggest': 0.28; 'suggestions': 0.28; 'it,': 0.29; 'attempt':
0.31; 'approach': 0.31; 'program,': 0.31; 'url-ip:188/8': 0.31;
'wondering': 0.31; 'think': 0.32; "doesn't": 0.32; 'empty': 0.32;
'python-list': 0.32; 'unless': 0.32; 'but': 0.32; 'subject:for':
0.33; 'there': 0.33; 'path': 0.33; 'same': 0.34; 'mean': 0.34;
'header:In-Reply-To:1': 0.34; 'trying': 0.35; 'english,': 0.35;
'handling': 0.35; 'item': 0.35; 'meaning': 0.35; 'yes,': 0.35;
'following': 0.35; 'people': 0.36; 'missing': 0.37; 'main': 0.37;
'using': 0.37; 'others': 0.37; 'this.': 0.37; 'way': 0.38;
'could': 0.38; 'quite': 0.39; 'above': 0.62; 'from:': 0.62; 'to:':
0.62; 'seen': 0.62; 'point.': 0.62; 'here': 0.62; 'destroy': 0.63;
'copy': 0.63; 'once': 0.63; 'pass': 0.64; 'complete': 0.64;
'clear': 0.64; 'imagine': 0.64; 'in:': 0.64; 're:': 0.64; 'thus':
0.64; 'your': 0.64; 'similar': 0.65; 'look': 0.65; 'well': 0.65;
'back': 0.67; 'body': 0.67; 'right': 0.68; 'matter': 0.68;
'further': 0.69; 'advantages': 0.69; 'change.': 0.69; 'clear.':
0.69; 'ignore': 0.71; 'content': 0.72; 'direct': 0.73; "you'll":
0.73; 'implemented': 0.76; 'sent:': 0.78; 'header:Reply-To:1':
0.79; 'out.': 0.80; 'leads': 0.81; 'clause': 0.84; 'exceptions':
0.84; 'integral': 0.84; 'parts.': 0.84; 'scope': 0.84; 'thing?':
0.84; 'differently': 0.91; 'retain': 0.91; 'implied': 0.93;
'green': 0.96
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=verizon.net; s=a2048;
t=1646427176; bh=JRCudEqNmXy5B2WFnvVx1LZeaOAnRGq/pgid2Rla4OE=;
h=Date:From:Reply-To:To:In-Reply-To:References:Subject:From:Subject:Reply-To;
b=Aoy7wZKs15JNMFh8kL4n9GJB5LYy21T/k9fBS0+P7EzInPBW8olk1N2j1JxB+ZUxp7K8dc2Tvi2Pg59nudtAJfA/vRkdQGZKvGQLG7GdKaIkUGMYzGhaUOsK7iwn/ZW9fKOlyN6HZCuDQ/Dna2rkIHBPqxK8/aVabJNz9ppQHN98lFyaZL8ntjzB5vXTrUBGiaaSYUrAxUCgyojAyOIuW10SX0XbyM1Cdt0EjiUx7ltfMxghZ6Gvpwni+g0KJCqWuF5DTZPyEqgJCkLvwdjflJrEHuoYKb7GC09WrakdWLHHE9pCLyiLaprxpfF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nKW.SlVNS_340mzikmDagid6Gt9l6MVpPjOPX4hOqn5bjNqqepJopmnPs2
kXho9rDlY6vUGmBBMGs8qkRgTZaHypVAiuEDpbMbC77ZtIAWD3WQ_Eg3EGv9RF6s7J_nwMJdkCHB
kZjIOHGjTuwHw7xv8AQzxehCgc6SMMr6akBD5dQF9S8IBTWhLmsZ7SC1QflcwzKacctRuPkl_5m8
LzvJSN6xIbNNko88pXdRP15ubTWkpiUv.mpcnZNiFJQm0lHN0wfcJYBy5cZ.5n8xwxnY_yw38oVE
wwf1k6bANsZkJfRkpmO6Cy1HwIRyjFko7mHTqB8si5aAAvit4qx2i4BmxQdOTGnRZol8q6qM1apR
quxlkb3cX7wZ7_LXEEH_0Jr4Xhv1DDlu7esdWuLU2cdQ0xeQDed8jdiIoDIdCAhgXeqwsxHrrCQx
wR5PpkN93C55R2kFSHDdFa_P.fyr8qAZOg4e6iwYQraIllGqs_I.3bsvNXKBnohbBMFk7lbfSAGN
msk1bPshI7iGceEIS878Noc7AqBTnoP60zdfrpTg.5fV7Z9BFgOP7ARVaxTJDhq775h.2G9GC6yb
bs01hkEwpkFfozRuJlm1kg9se2E3qI_DDj6MqLM8w5EnGOwvqpQcu7hRPbMgHTTtpuD7njUrTLAW
i5pk-
X-Sonic-MF: <avigross@verizon.net>
In-Reply-To: <CAPTjJmpGph9woqQGnkoE9=auFw49UqYMr3OxbhK24LytUgdyEA@mail.gmail.com>
X-Mailer: WebService/1.1.19797 aolwebmail
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: <1957563644.11129.1646427163819@mail.yahoo.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>
<CAPTjJmpGph9woqQGnkoE9=auFw49UqYMr3OxbhK24LytUgdyEA@mail.gmail.com>
 by: Avi Gross - Fri, 4 Mar 2022 20:52 UTC

Chris,

My example was precisely what to do when it is an empty closet:

> 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!!")

Of course, for a more complete program, there is a reason to break out of either loop if you find what you are looking for. I was illustrating an alternate use in what I agree is a very incomplete example as it was meant only to illustrate one point.

And my example may be implemented very differently if you need to search in dozens of places. Consider the ubiquitous PATH variable. Many programs search in one after another of the components for something like a filename. Doing that with a series of nested FOR loops with each one nested in the ELSE of the previous one might be a tad ridiculous!

I have an observation about exception handling in general. Some people use exceptions, including ones they create and throw, for a similar idea. You might for example try to use an exception if your first attempt fails that specifies trying the next attempt. In my example of the PATH variable, you might use something like calling a function and giving it what you are looking for as well as a local copy of the PATH variable content and the exception would be to call the function again with the first component of PATH removed until you fail with an empty PATH. Yes, this is similar to just having a recursive function.

But I am hoping to get across my view that there are many ways to do things but sometimes a new feature is useful because it is more direct or other reasons. in the case we are discussing, what are the scope issues in:

eye = 1
for eye in ... :
pass
else:
eye = None

It looks like all references to eye refer to the same object. Some other methods we have touched on (or others may bring up) may result in different results as variables may be in other name spaces. My understanding of the PYTHON-only implementation of the above is something like this. Imagine a FOR loop that does not ignore the contents no matter what and the body looks like:

if SOMETHING_IS_BEING_DONE:
main code processing eye sequentially
else: # only used if nothing is being done normally
eye = None

The above made-up scenario would allow the external ELSE to be seen as the implied else to an if that tests if the loop was actually entered. Of course it is not implemented this way, but it would show why variable eye is in the same scope in the several parts. Once you play with exceptions and other functions, this may change.

The alternative suggestions about ELSE being associated confuse this issue so I want to be clear.

The main meaning of the ELSE clause does not sound quite like this. It is actually about what to do with a loop that terminates normally, as I think Chris pointed out. This example finishes the loop normally and then does the ELSE:

for x in range(6):
print(x)
else:
print("Finally finished!")

The following example is similar but quits early:

for x in range(6):
if x < 4: print(x)
else: break
else:
print("Finally finished!")

The (second) else clause is not run.

So the example tossed at us looks a bit more like this and it does run the ELSE not because the loop is not done but because the loop never calls a break:

for x in range(0):
print(x)
else:
print("Finally finished!")

So unless I am missing something, the code would destroy the last value of the variable even in a valid case when it looks like:

for aye in range(0):
pass
else:
aye = None

I mean if you used range(5) you would expect the value of aye==4 after the loop. The above loop would leave it as aye==None which keeps it from being undefined. To decide if the loop ran at all would thus require further code such as:

if aye == None:
...

Which leads me right back to wondering why the sentinel approach is so bad!

-----Original Message-----
From: Chris Angelico <rosuav@gmail.com>
To: python-list@python.org <python-list@python.org>
Sent: Fri, Mar 4, 2022 12:47 pm
Subject: Re: Behavior of the for-else construct

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

--
https://mail.python.org/mailman/listinfo/python-list

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor