Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

The nation that controls magnetism controls the universe. -- Chester Gould/Dick Tracy


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

SubjectAuthor
o Re: Behavior of the for-else constructRoel Schroeven

1
Re: Behavior of the for-else construct

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: roe...@roelschroeven.net (Roel Schroeven)
Newsgroups: comp.lang.python
Subject: Re: Behavior of the for-else construct
Date: Thu, 3 Mar 2022 18:11:13 +0100
Lines: 103
Message-ID: <mailman.125.1646327477.2329.python-list@python.org>
References: <CALq4Z0-fJk-HOu0ka2kPrOioPYAh3e3zbziwetUDmAAx1U1LMw@mail.gmail.com>
<cc255541-5478-f677-2011-27bbd287af9c@roelschroeven.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de 0+LJwBps6kqooisXqT6mXwZgqeg73hyrC3Tng2Tju6Hw==
Return-Path: <roel@roelschroeven.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=roelschroeven.net header.i=@roelschroeven.net
header.b=a9Mmprtf; dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.012
X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'python?': 0.03; 'this:':
0.03; '(which': 0.04; 'def': 0.04; 'parallel': 0.05; 'achieved':
0.07; 'explanation': 0.07; 'loop': 0.07; 'skip:\xc2 30': 0.07;
'construct': 0.09; 'else:': 0.09; 'example:': 0.09; 'intelligent':
0.09; 'skip:\xc2 20': 0.09; 'tutorial': 0.12; 'coding': 0.13;
'3.x': 0.16; 'construct.': 0.16; 'executed': 0.16; 'iterable':
0.16; 'once.': 0.16; 'reasons."': 0.16; 'received:10.202': 0.16;
'received:10.202.2': 0.16; 'received:10.202.2.163': 0.16;
'received:internal': 0.16; 'received:messagingengine.com': 0.16;
'refactor': 0.16; 'schreef': 0.16; 'sensible': 0.16;
'subject:else': 0.16; 'unfortunate': 0.16; 'universe,': 0.16;
'useful.': 0.16; 'whilst': 0.16; 'python': 0.16; 'reached': 0.17;
'code.': 0.17; "can't": 0.17; 'proposals': 0.19; 'to:addr:python-
list': 0.20; 'issue': 0.21; 'maybe': 0.22; '\xe2\x80\x94': 0.22;
'code': 0.23; 'skip:p 30': 0.23; 'do,': 0.26; 'interface': 0.26;
'so.': 0.26; 'creating': 0.27; 'else': 0.27; 'bit': 0.27;
'function': 0.27; 'done': 0.28; 'example,': 0.28; 'header:User-
Agent:1': 0.30; 'seem': 0.31; 'flow': 0.31; 'looked': 0.31;
'program': 0.31; 'think': 0.32; "doesn't": 0.32; 'dive': 0.32;
'empty': 0.32; "wouldn't": 0.32; 'but': 0.32; "i'm": 0.33;
'subject:for': 0.33; 'there': 0.33; 'same': 0.34; 'printing':
0.34; 'header:In-Reply-To:1': 0.34; 'received:66': 0.35; 'also,':
0.36; 'functions': 0.36; 'possibly': 0.36; 'necessarily': 0.37;
"it's": 0.37; 'hard': 0.37; 'way': 0.38; 'means': 0.38; 'least':
0.39; 'single': 0.39; 'otherwise': 0.39; 'list': 0.39; 'use':
0.39; 'block': 0.39; 'break': 0.39; 'something': 0.40; 'want':
0.40; 'best': 0.61; 'remember': 0.61; 'here.': 0.61; 'me.': 0.62;
'seen': 0.62; 'skip:\xc2 10': 0.62; 'true': 0.63; 'ever': 0.63;
'simply': 0.63; 'feel': 0.63; 'clear': 0.64; 'imagine': 0.64;
'in.': 0.64; 'time.': 0.66; 'now,': 0.67; 'exactly': 0.68;
'know.': 0.68; 'compare': 0.69; 'mix': 0.69; 'survey': 0.71;
'little': 0.73; 'head': 0.73; 'man': 0.74; 'name,': 0.75;
'8bit%:100': 0.76; 'yes': 0.76; 'business': 0.77; 'major': 0.78;
'water': 0.81; 'balanced': 0.84; 'behavior:': 0.84; 'dolphins':
0.84; 'driver': 0.84; 'ele': 0.84; 'expresses': 0.84; 'this)':
0.84; '\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0': 0.84; 'adams':
0.91; 'avoiding': 0.91; 'broke': 0.91; 'believed': 0.93;
'consists': 0.93; 'url-ip:2.19/16': 0.93; 'url-ip:2/8': 0.95
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=
roelschroeven.net; h=cc:content-transfer-encoding:content-type
:date:date:from:from:in-reply-to:in-reply-to:message-id
:mime-version:references:reply-to:sender:subject:subject:to:to;
s=fm2; bh=VNwsGE34UeOJz15GCbClFm8SSsLMBYxWzvFRwcbYqmc=; b=a9Mmp
rtfy5rDXrZ8Xsh0Gr9UoxQzVDGp5fjXlCV329sXHCaGNPNqM/+7aC31Iv1VlDd/X
HZkqS+bZoElOJWXr+02vUiMXPVc1QpPk6wwIrDemk7LkjQ4014CloDDxhy5FBnQ0
eN/Gkk2u9TqsCxNvs0YTTkbgiYm1Ulcn+sSXfv5jxvEVcgjEivybgZ87M4j4f15v
mfvp0NMs3+VTPU4reskNp7jPk0+F7lQVtsYUFqLrld4X7whD8/28Jxv458CzPiNo
VeCWCgjVlLZNRN364dSBEhpVBuiMLu11FMkOcdyGThyysEH9sQ9tmJhSu5xuEjb0
WX7UBqDY4ZChyDRzA==
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=
messagingengine.com; h=cc:content-transfer-encoding:content-type
:date:date:from:from:in-reply-to:in-reply-to:message-id
:mime-version:references:reply-to:sender:subject:subject:to:to
:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=
fm2; bh=VNwsGE34UeOJz15GCbClFm8SSsLMBYxWzvFRwcbYqmc=; b=VMbi9fvt
z0JNhQ6eZEuYTTN1AYjEgji9gAYqDfT8me1l1XjhmFUAslIk6Zy15hUv9gUH1UEL
ITbqdeC1KuRReQCGlhL0QkL0n7Og8T3+07MPkbAyndPgudrLo/+RkHRkTa17zLo/
JE1oIByUBcYshiYETBPPEVA3pb3c1p8mXoCbgeK1gcgpewnzA05gRxh16+9dIC/w
twt3hb183xb5dqqS0Yj2lsrhRlyfqfA5KhSnIV+UNVd3yb4Wy7YXLvGbuq9p4ahn
gtM2w188QMzPtgtMTY6we/xJx1EpF1QvGYXuG15QqpS3uyZJrinHJO6+EUXyi9g3
hwo/nnKPMTtNYQ==
X-ME-Sender: <xms:sfYgYqljMNfqQhmxgxlEQ2LoOPnVBwPHLqI2F218cuVnZyfvw66_8w>
<xme:sfYgYh1h5s7oOq_vRJDSWyExoWpVT6l9P2bCZvT4K_tSnRLBHGAZkKA2zk9zfUnG8
O8ZR-EQQ3uo>
X-ME-Received: <xmr:sfYgYoq_c7fTW2KD6tV-UiN9oZFwUZJc8EEnTov0b6FoCLoC4xAgUf_BHrB9ZF_EB1tiqymXbWvgLv7gVYnFBCsa1SR9aZNn10yPW9sK_evz6IA>
X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvvddruddtiedgleeiucetufdoteggodetrfdotf
fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen
uceurghilhhouhhtmecufedttdenucenucfjughrpefkffggfgfuvfhfhfgjtgfgsehtke
ertddtfeejnecuhfhrohhmpeftohgvlhcuufgthhhrohgvvhgvnhcuoehrohgvlhesrhho
vghlshgthhhrohgvvhgvnhdrnhgvtheqnecuggftrfgrthhtvghrnheptdeffeelfeduhe
ejhffggfektddvhffgffejudelheejheelieelhefhffejjedunecuffhomhgrihhnpehg
vggvkhhsfhhorhhgvggvkhhsrdhorhhgnecuvehluhhsthgvrhfuihiivgeptdenucfrrg
hrrghmpehmrghilhhfrhhomheprhhovghlsehrohgvlhhstghhrhhovghvvghnrdhnvght
X-ME-Proxy: <xmx:sfYgYun1eZ4AihHDQA5ta_EjMnL8Sr1Z39cQK_Wx1R9UikmGX7KknA>
<xmx:sfYgYo3O7AmMENuz2U2-Z20Bd8vxSL8xtYtVEuP2fuX49cdz9t463w>
<xmx:sfYgYls8SSyJI-CD2WXcINDzcl9_ETsr1I1YwTdrZVHmOZJTvNH_2Q>
<xmx:svYgYpi6-D2nXkKav53bVl3TDcdDgUaSJhmxuCkd9B68JLBFT5-2oQ>
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.6.1
Content-Language: en-US
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: <cc255541-5478-f677-2011-27bbd287af9c@roelschroeven.net>
X-Mailman-Original-References: <CALq4Z0-fJk-HOu0ka2kPrOioPYAh3e3zbziwetUDmAAx1U1LMw@mail.gmail.com>
 by: Roel Schroeven - Thu, 3 Mar 2022 17:11 UTC

Op 3/03/2022 om 14:24 schreef computermaster360:
> 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?
- No, or at least not when balanced against the drawbacks as I perceive
them.
- I have a hard time remembering how it works, which is the major
drawback for me. I simply can't seem to wrap my head around it.
- Yes
> 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`.
There have been multiple proposals for a better name, but they all have
their own drawbacks. There doesn't seem to be a clear winner.

Maybe a bit of an explanation of my dislike for the for-else construct.
I can't remember what exactly it does and doesn't do, maybe because of
the IMHO unfortunate naming or maybe because it's just an unusal
construct, I don't know. While coding I never feel the need for for-else.
I like coding in a way where "every line expresses a single thought", as
much as possible. That means that I don't mine creating short functions
for things that in my mind otherwise break the flow of my code. So I
often have short functions with a for loop and early returns. I have
never seen an example of for-else that wouldn't be at least as clear
with a little helper function (granted I haven't looked all that hard).

I'm googling for an example, but all I find are tutorial-type things
that don't necessarily compare to real code. But it's the best I can do,
so let's dive in. What they all seem to have in common is that they mix
business logic and user interface (maybe precisely because of their
tutorial nature), which I strive to avoid. For example, on
https://www.geeksforgeeks.org/using-else-conditional-statement-with-for-loop-in-python/
I found this example:

    # Python 3.x program to check if an array consists
    # of even number
    def contains_even_number(l):
      for ele in l:
        if ele % 2 == 0:
          print ("list contains an even number")
          break

      # This else executes only if break is NEVER
      # reached and loop terminated after all iterations.
      else:
        print ("list does not contain an even number")

    # Driver code
    print ("For List 1:")
    contains_even_number([1, 9, 8])
    print (" \nFor List 2:")
    contains_even_number([1, 3, 5])

I would always refactor this even just for that mixing of business logic
and printing stuff. Perhaps like this:

    def contains_even_number(lst):
        for element in lst:
            if element % 2 == 0:
                return True
        return False

    def print_contains_even_number(lst):
        if contains_even_number(lst):
            print("list contains an even number")
        else:
            print("list does not contain an even number")

    print("For List 1:")
    print_contains_even_number([1, 9, 8])
    print()
    print("For List 2:")
    print_contains_even_number([1, 3, 5])

Even without consciously avoiding for-else, it automatically disappears.
(In real code I would actually use any(element % 2 == 0 for element in
lst) for something simple like this)

> 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).
That's a different construct, also possibly useful. I think this would
be more useful to me than the existing for-else.

--
"Man had always assumed that he was more intelligent than dolphins because
he had achieved so much — the wheel, New York, wars and so on — whilst all
the dolphins had ever done was muck about in the water having a good time.
But conversely, the dolphins had always believed that they were far more
intelligent than man — for precisely the same reasons."
-- Douglas Adams

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor