Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #63231 > unrolled thread
| Started by | Göktuğ Kayaalp <self@gkayaalp.com> |
|---|---|
| First post | 2014-01-06 00:12 +0200 |
| Last post | 2014-01-06 00:12 +0200 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Fwd: Re: Postfix conditionals Göktuğ Kayaalp <self@gkayaalp.com> - 2014-01-06 00:12 +0200
| From | Göktuğ Kayaalp <self@gkayaalp.com> |
|---|---|
| Date | 2014-01-06 00:12 +0200 |
| Subject | Fwd: Re: Postfix conditionals |
| Message-ID | <mailman.4975.1388959945.18130.python-list@python.org> |
[Multipart message — attachments visible in raw view] — view raw
This was sent to me as a private reply to a question that I have posted
to python-list@python.org, so I am forwarding it to here.
Chris, please send your messages to the list, and cc the OP.
-------- Original Message --------
Subject: Re: Postfix conditionals
Date: Sun, 5 Jan 2014 14:09:14 -0800
From: Chris Rebert <clp2@rebertia.com>
To: Göktuğ Kayaalp <self@gkayaalp.com>
CC: Python <python-list@python.org>
On Sun, Jan 5, 2014 at 12:24 PM, Göktuğ Kayaalp <self@gkayaalp.com> wrote:
> Hi,
>
> AFAIK, we do not have "postfix conditionals" in Python, i.e. a condition
> appended to a
> statement, which determines whether the statement runs or not:
>
> py> for i in [False]:
> ... break if not i
>
> The above piece of code is equivalent to this in Python:
>
> py> for i in [False]:
> ... if not i
> ... break
>
> I believe that the first example is superior to the second example when the
> two is compared
> for readability and intuitiveness.
I'm going to have to disagree. I dislike how this obscures the
if-statement, complicates the language grammar, and adds another
unnecessary way to express the same thing (which violates TOOWTDI)
with little countervailing benefit.
> We already have a ternary statement that
> looks similar,
>
> py> print('hi') if True else None
Actually, to be pedantic, it's a ternary *expression*. Using it purely
for side-effects (i.e. as a statement) is rather unidiomatic, in the
same way that abusing list comprehensions, e.g.:
[print(i) for i in range(42)]
is frowned upon.
Not to mention that the ternary doesn't work for actual statements
(print() is just a function call in Python 3):
>>> (x = 1) if True else (x = 2)
File "<stdin>", line 1
(x = 1) if True else (x = 2)
^
SyntaxError: invalid syntax
> so I reckon there would be no breakage in old code if this kind of syntax
> was added. Ruby has
> this, and AFAIK Perl also does.
>
> I lack the knowledge of whether the community has opinions on this kind of
> notation, so I am
> posting this here instead of the ideas list. What are your thoughts on
> this?
You can already write:
for i in [False]:
if not i: break
if you feel the need for terseness or a one-liner. Perhaps this
satisfies your desire?
Cheers,
Chris
Back to top | Article view | comp.lang.python
csiph-web