Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Deliver yesterday, code today, think tomorrow.


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

SubjectAuthor
o Re: Behavior of the for-else constructPeter J. Holzer

1
Re: Behavior of the for-else construct

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: hjp-pyt...@hjp.at (Peter J. Holzer)
Newsgroups: comp.lang.python
Subject: Re: Behavior of the for-else construct
Date: Sat, 5 Mar 2022 00:11:50 +0100
Lines: 86
Message-ID: <mailman.178.1646435511.2329.python-list@python.org>
References: <YiFCPlGC+2aRIR0K@shallowsky.com>
<21739669.459456.1646348879560@mail.yahoo.com>
<e3544242-cd54-05d3-2101-5b9fedc1c13e@btinternet.com>
<621325684.471007.1646354302946@mail.yahoo.com>
<CAPTjJmpNT4TTRiYRD2G73EsDp3O+_Tg_sF2vp3Tf0vC-EJWqAA@mail.gmail.com>
<20220304083852.28142cce@bigbox.attlocal.net>
<CAPTjJmrNeOKRan_1w-du5M_EygmMiz0jct4mGQ3B6QQBS_=i+Q@mail.gmail.com>
<998429825.641242.1646423036979@mail.yahoo.com>
<1017363129.639920.1646423121331@mail.yahoo.com>
<17f5688eb9e.1035c37393512819.582614813492286794@omajoshi.com>
<20220304231150.mencyhgmfpzujqdi@hjp.at>
Mime-Version: 1.0
Content-Type: multipart/signed; micalg=pgp-sha512;
protocol="application/pgp-signature"; boundary="ui4ctpo6f432oc2b"
X-Trace: news.uni-berlin.de erwTWMoYYCFExe6XRtGjwwZfblnVIrffxvJNri17Qn8w==
Return-Path: <hjp-python@hjp.at>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.000
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'content-
type:multipart/signed': 0.05; 'django': 0.07; 'template': 0.07;
'content-type:application/pgp-signature': 0.09; 'filename:fname
piece:asc': 0.09; 'filename:fname piece:signature': 0.09;
'filename:fname:signature.asc': 0.09; 'syntax': 0.15; '"creative':
0.16; '__/': 0.16; 'anyway.': 0.16; 'challenge!"': 0.16; 'empty,':
0.16; 'frequently': 0.16; 'from:addr:hjp-python': 0.16;
'from:addr:hjp.at': 0.16; 'from:name:peter j. holzer': 0.16;
'hjp@hjp.at': 0.16; 'holzer': 0.16; 'item,': 0.16; 'joshi': 0.16;
'reality.': 0.16; 'sorry,': 0.16; 'stross,': 0.16; 'subject:else':
0.16; 'url-ip:212.17.106.137/32': 0.16; 'url-ip:212.17.106/24':
0.16; 'url-ip:212.17/16': 0.16; 'url:hjp': 0.16; '|_|_)': 0.16;
'wrote:': 0.16; 'uses': 0.19; 'to:addr:python-list': 0.20;
'anyone': 0.25; 'seems': 0.26; 'else': 0.27; 'sense': 0.28;
'comment': 0.31; 'empty': 0.32; 'but': 0.32; "i'm": 0.33;
'subject:for': 0.33; 'header:In-Reply-To:1': 0.34; 'respect':
0.35; 'quite': 0.39; 'adding': 0.39; 'least': 0.39; 'single':
0.39; 'text': 0.39; 'mentioned': 0.39; 'list': 0.39; 'want': 0.40;
'received:212': 0.62; 'your': 0.64; 'well': 0.65;
'received:userid': 0.66; 'url-ip:212/8': 0.69; 'html': 0.80;
'produce:': 0.84; 'received:at': 0.84; 'want.': 0.84
Content-Disposition: inline
In-Reply-To: <17f5688eb9e.1035c37393512819.582614813492286794@omajoshi.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: <20220304231150.mencyhgmfpzujqdi@hjp.at>
X-Mailman-Original-References: <YiFCPlGC+2aRIR0K@shallowsky.com>
<21739669.459456.1646348879560@mail.yahoo.com>
<e3544242-cd54-05d3-2101-5b9fedc1c13e@btinternet.com>
<621325684.471007.1646354302946@mail.yahoo.com>
<CAPTjJmpNT4TTRiYRD2G73EsDp3O+_Tg_sF2vp3Tf0vC-EJWqAA@mail.gmail.com>
<20220304083852.28142cce@bigbox.attlocal.net>
<CAPTjJmrNeOKRan_1w-du5M_EygmMiz0jct4mGQ3B6QQBS_=i+Q@mail.gmail.com>
<998429825.641242.1646423036979@mail.yahoo.com>
<1017363129.639920.1646423121331@mail.yahoo.com>
<17f5688eb9e.1035c37393512819.582614813492286794@omajoshi.com>
 by: Peter J. Holzer - Fri, 4 Mar 2022 23:11 UTC
Attachments: signature.asc (application/pgp-signature)

On 2022-03-04 14:04:48 -0600, Om Joshi wrote:
> I'm not sure if anyone has mentioned it on this thread, but with
> respect to your comment about adding either on.empty or a decorator,
> the Django template syntax uses
>
> {% for x in iterator %}
> <h2>{{ x }}</h2>
> {% empty %}
> <h2>Empty</h2>
> {% endfor %}
>
> and this seems to work quite well and be incredibly intuitive, at
> least for Django html templates.

OTOH it is frequently not what you want.

Take this example from the Django docs:

<ul>
{% for athlete in athlete_list %}
<li>{{ athlete.name }}</li>
{% empty %}
<li>Sorry, no athletes in this list.</li>
{% endfor %}
</ul>

If athlete_list is empty, it will produce:

<ul>
<li>Sorry, no athletes in this list.</li>
</ul>

which is awful typography. You don't want a list with a single item, you
want that text *instead of* the list.

So you would have to write

{% if athlete_list %}
<ul>
{% for athlete in athlete_list %}
<li>{{ athlete.name }}</li>
{% empty %}
</ul>
{% else %}
<div class="whatever">
Sorry, no athletes in this list.
</div>
{%endif %}

anyway.

hp

--
_ | Peter J. Holzer | Story must make more sense than reality.
|_|_) | |
| | | hjp@hjp.at | -- Charles Stross, "Creative writing
__/ | http://www.hjp.at/ | challenge!"

Attachments: signature.asc (application/pgp-signature)
1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor