471,873 Members | 1,835 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,873 software developers and data experts.

python syntax for conditional is unfortunate

In hindsight, I am disappointed with the choice of conditional syntax. I know it's too late to change. The problem is

y = some thing or other if x else something_else

When scanning this my eye tends to see the first phrase and only later notice that it's conditioned on x (or maybe not notice at all!). Particularly if 'some thing or other' is long or complicated.
Sep 23 '08 #1
13 1448
On Sep 23, 6:52*pm, Neal Becker <ndbeck...@gmail.comwrote:
In hindsight, I am disappointed with the choice of conditional syntax. *I know it's too late to change. *The problem is

y = some thing or other if x else something_else

When scanning this my eye tends to see the first phrase and only later notice that it's conditioned on x (or maybe not notice at all!). *Particularly if 'some thing or other' is long or complicated.
You're talking strictly about readability, which among other things is
in the eye of the beholder, of course. Temporary variables can clean
up some code, even if choosing names can be a hassle and it's more
things to keep track of. Long lines and extra variables form a trade-
off. You are writing a line with a conditional expression the value
of which depends on something. What does it depend on, what is its
value if that's true, and what is it if it's false? '...if...else...'
only takes 6 characters... maybe you want more!

If you're looking for a strictly syntactic construct, you can always
"fire blanks", or tracers, if the analogy's more accurate.

z= conditionally( x ) if b else y

This could serve as a gentle reminder, even where 'conditionally'
returns its argument, i.e. is the identity function. You can always
roll your own ternary with extra parameters too:

z= condition( b, x, y )

Just don't confuse it with threading.Condition.

Otherwise, you're stuck with old syntax markers, and unless you
wanted:

z= if b then x else y

You're out of options. You have to express it somehow. Did you want
the condition first? Was there an alternative proposal you
preferred? IINM if I'm not mistaken,

z= b and x or y

works just the same so long as x evaluates to True, as it will be
tested.

Feel free to write your own from scratch, and we'll see how close
Python can come to resembling it.

I suppose you can compare it to someone who stops listening before the
word 'if', and completely misunderstands your statement. "Feed the
dog if he's standing near the food dish" != "Feed the dog", which can
of course lead to errors of both omission and commission (doing too
little -or- too much). There's no way to fix that in a guaranteed
way, except to say, "listen to the whole statement".

And strictly sarcastically, what did you want to do with reading the
program? Why were you reading it? <snicker, ducks>
Sep 24 '08 #2
On 23 set, 20:52, Neal Becker <ndbeck...@gmail.comwrote:
In hindsight, I am disappointed with the choice of conditional syntax. *I know it's too late to change. *The problem is

y = some thing or other if x else something_else

When scanning this my eye tends to see the first phrase and only later notice that it's conditioned on x (or maybe not notice at all!). *Particularly if 'some thing or other' is long or complicated.
Yes, infix syntax sucks. No ambiguities in prefixed Lisp: (if x (or
something other) something_else)

Anyway, pretty amusing seeing Guido nodding to Larry... ;)
Sep 24 '08 #3
Aaron "Castironpi" Brady wrote:
On Sep 23, 6:52 pm, Neal Becker <ndbeck...@gmail.comwrote:
>In hindsight, I am disappointed with the choice of conditional syntax. I
know it's too late to change. The problem is

y = some thing or other if x else something_else

When scanning this my eye tends to see the first phrase and only later
notice that it's conditioned on x (or maybe not notice at all!).
Particularly if 'some thing or other' is long or complicated.

You're talking strictly about readability, which among other things is
in the eye of the beholder, of course. Temporary variables can clean
up some code, even if choosing names can be a hassle and it's more
things to keep track of. Long lines and extra variables form a trade-
off. You are writing a line with a conditional expression the value
of which depends on something. What does it depend on, what is its
value if that's true, and what is it if it's false? '...if...else...'
only takes 6 characters... maybe you want more!

If you're looking for a strictly syntactic construct, you can always
"fire blanks", or tracers, if the analogy's more accurate.

z= conditionally( x ) if b else y

This could serve as a gentle reminder, even where 'conditionally'
returns its argument, i.e. is the identity function. You can always
roll your own ternary with extra parameters too:

z= condition( b, x, y )

Just don't confuse it with threading.Condition.

Otherwise, you're stuck with old syntax markers, and unless you
wanted:

z= if b then x else y

You're out of options. You have to express it somehow. Did you want
the condition first? Was there an alternative proposal you
preferred? IINM if I'm not mistaken,

z= b and x or y

works just the same so long as x evaluates to True, as it will be
tested.

Feel free to write your own from scratch, and we'll see how close
Python can come to resembling it.

I suppose you can compare it to someone who stops listening before the
word 'if', and completely misunderstands your statement. "Feed the
dog if he's standing near the food dish" != "Feed the dog", which can
of course lead to errors of both omission and commission (doing too
little -or- too much). There's no way to fix that in a guaranteed
way, except to say, "listen to the whole statement".

And strictly sarcastically, what did you want to do with reading the
program? Why were you reading it? <snicker, ducks>
I find I'm often tripped up by:

x = Y (lots of constructor arguments....) if something ...

on first glance, I don't notice the if.

Why am I reading this? Umm, because I wrote it.

Sep 24 '08 #4
On Sep 23, 8:50*pm, Neal Becker <ndbeck...@gmail.comwrote:
Aaron "Castironpi" Brady wrote:
On Sep 23, 6:52 pm, Neal Becker <ndbeck...@gmail.comwrote:
In hindsight, I am disappointed with the choice of conditional syntax.*I
know it's too late to change. *The problem is
y = some thing or other if x else something_else
When scanning this my eye tends to see the first phrase and only later
notice that it's conditioned on x (or maybe not notice at all!).
Particularly if 'some thing or other' is long or complicated.
You're talking strictly about readability, which among other things is
in the eye of the beholder, of course. *Temporary variables can clean
up some code, even if choosing names can be a hassle and it's more
things to keep track of. *Long lines and extra variables form a trade-
off. *You are writing a line with a conditional expression the value
of which depends on something. *What does it depend on, what is its
value if that's true, and what is it if it's false? *'...if...else...'
only takes 6 characters... maybe you want more!
If you're looking for a strictly syntactic construct, you can always
"fire blanks", or tracers, if the analogy's more accurate.
z= conditionally( x ) if b else y
This could serve as a gentle reminder, even where 'conditionally'
returns its argument, i.e. is the identity function. *You can always
roll your own ternary with extra parameters too:
z= condition( b, x, y )
Just don't confuse it with threading.Condition.
Otherwise, you're stuck with old syntax markers, and unless you
wanted:
z= if b then x else y
You're out of options. *You have to express it somehow. *Did you want
the condition first? *Was there an alternative proposal you
preferred? *IINM if I'm not mistaken,
z= b and x or y
works just the same so long as x evaluates to True, as it will be
tested.
Feel free to write your own from scratch, and we'll see how close
Python can come to resembling it.
I suppose you can compare it to someone who stops listening before the
word 'if', and completely misunderstands your statement. *"Feed the
dog if he's standing near the food dish" != "Feed the dog", which can
of course lead to errors of both omission and commission (doing too
little -or- too much). *There's no way to fix that in a guaranteed
way, except to say, "listen to the whole statement".
And strictly sarcastically, what did you want to do with reading the
program? *Why were you reading it? *<snicker, ducks>

I find I'm often tripped up by:

x = Y (lots of *constructor arguments....) if something ...

on first glance, I don't notice the if.

Why am I reading this? *Umm, because I wrote it.
Put it somewhere you'll notice!
Sep 24 '08 #5
On 23 set, 22:50, Neal Becker <ndbeck...@gmail.comwrote:
I find I'm often tripped up by:

x = Y (lots of *constructor arguments....) if something ...

on first glance, I don't notice the if.
Nobody does. This peculiar syntax has much better usage in short
expressions. dothis if this else dothat
Sep 24 '08 #6
"Aaron \"Castironpi\" Brady" <ca********@gmail.comwrote:
You're out of options. You have to express it somehow.
How about:

Assignith z the value of x if the value of b is such that it is true, else
assignith it the value of y. Assignith z not the value of w, nor the value
of v, lest you raise NameError upon thy stack trace.
Sep 24 '08 #7
Neal Becker a écrit :
(snip)
I find I'm often tripped up by:

x = Y (lots of constructor arguments....) if something ...

on first glance, I don't notice the if.
Indeed. The inline conditionnal syntax is obviously innappropriate here.
It's just like list-comprehensions : just fine for simple use case,
definitively not appropriate when it comes to anything complex. IOW :
it's not the syntax that's unfortunate, it's the coding style that's wrong.

My 2 cents...

Sep 24 '08 #8
On Sep 24, 9:52 am, Neal Becker <ndbeck...@gmail.comwrote:
In hindsight, I am disappointed with the choice of conditional syntax. I know it's too late to change. The problem is

y = some thing or other if x else something_else

When scanning this my eye tends to see the first phrase and only later notice that it's conditioned on x (or maybe not notice at all!). Particularly if 'some thing or other' is long or complicated.
The struggle to get a conditional operator was a long and bitter, so
in the first place we should be glad we aren't writing "y =
(conditional and [p] or [q])[0] anymore. Since it was but grudgingly
bestowed I thought BDFL had chosen this particular syntax just to be
difficult.

However since using it for a while, I am surprised how natural it is
to use and read. A canonical use of the conditional operator is in
pluralising words, (eg. '%s dollar' % n + 's' if n!=1 else ''). For
this and similar short uses, where the regular if statement is an
annoyance this syntax <ususal_caseif <conditionselse
<unusual_caseworks nicely. More complicated conditionals or cases
are probably better handled by an if statement. This syntax is also
probably not the best for nested conditionals. The latter, however,
is probably feature rather than bug.
Sep 24 '08 #9
Asun Friere <af*****@yahoo.co.ukwrites:
A canonical use of the conditional operator is in
pluralising words, (eg. '%s dollar' % n + 's' if n!=1 else '').
That fails for n == 1. So what is best?

for i in range(4):
print '%d thing' % i + ('s' if i != 1 else '')

for i in range(4):
print '%d thing%s' % (i, ('s', '')[i==1])

for i in range(4):
print '%d thing%s' % (i, 's' if i != 1 else '')
--
Pete Forman -./\.- Disclaimer: This post is originated
WesternGeco -./\.- by myself and does not represent
pe*********@westerngeco.com -./\.- the opinion of Schlumberger or
http://petef.22web.net -./\.- WesternGeco.
Sep 24 '08 #10
On Sep 25, 3:16 am, Pete Forman <pete.for...@westerngeco.comwrote:
Asun Friere <afri...@yahoo.co.ukwrites:
A canonical use of the conditional operator is in
pluralising words, (eg. '%s dollar' % n + 's' if n!=1 else '').

That fails for n == 1. So what is best?
Sorry missing parentheses. I should test before posting, even for
code written into.
for i in range(4):
print '%d thing' % i + ('s' if i != 1 else '')
That is the correct version of what I meant, but your last, including
all variables for placeholders in the tuple is probably better.
Sep 25 '08 #11
On Sep 25, 3:16 am, Pete Forman <pete.for...@westerngeco.comwrote:
Asun Friere <afri...@yahoo.co.ukwrites:
A canonical use of the conditional operator is in
pluralising words, (eg. '%s dollar' % n + 's' if n!=1 else '').

That fails for n == 1. So what is best?
Sorry missing parantheses. I should test, even for fragments written
out as part of a sentence. %-/
for i in range(4):
print '%d thing' % i + ('s' if i != 1 else '')
That's the corrected version of what I meant, but actually I think
your last version ('%d thing%s' % (i, 's' if i != 1 else '')), holding
all variables for placeholders in the tuple, is better. It's certainly
more readible.
Sep 25 '08 #12
On Sep 24, 8:40*pm, Asun Friere <afri...@yahoo.co.ukwrote:
On Sep 25, 3:16 am, Pete Forman <pete.for...@westerngeco.comwrote:
Asun Friere <afri...@yahoo.co.ukwrites:
*A canonical use of the conditional operator is in
*pluralising words, (eg. '%s dollar' % n + 's' if n!=1 else '').
That fails for n == 1. *So what is best?

Sorry missing parantheses. *I should test, even for fragments written
out as part of a sentence. %-/
for i in range(4):
* * print '%d thing' % i + ('s' if i != 1 else '')

That's the corrected version of what I meant, but actually I think
your last version ('%d thing%s' % (i, 's' if i != 1 else '')), holding
all variables for placeholders in the tuple, is better. It's certainly
more readible.
It's a different answer if you have 'things is/are'. '%d thing%s %s'%
( ( i, )+ ( 's', 'are' ) if i!= 1 else ( '', 'is' ) ). Or excluding
prepositional phrases and subordinate clauses, '%d thing%s'% ( i, 's
are' if i!= 1 else ' is' ).
Sep 25 '08 #13
On Sep 25, 11:57 am, "Aaron \"Castironpi\" Brady"
<castiro...@gmail.comwrote:
On Sep 24, 8:40 pm, Asun Friere <afri...@yahoo.co.ukwrote:
... I think
your last version ('%d thing%s' % (i, 's' if i != 1 else '')), holding
all variables for placeholders in the tuple, is better. It's certainly
more readible.

It's a different answer if you have 'things is/are'. '%d thing%s %s'%
( ( i, )+ ( 's', 'are' ) if i!= 1 else ( '', 'is' ) ). Or excluding
prepositional phrases and subordinate clauses, '%d thing%s'% ( i, 's
are' if i!= 1 else ' is' ).
Forgive me for being dull, my caffeine levels have not yet optimal,
but I don't follow. Both the solutions you propose do put all the
placeholder variables in the tuple. Or are you saying it no longer
remains readible?

BTW you repeated my mistake with the first scraplet of code.

Sep 25 '08 #14

This discussion thread is closed

Replies have been disabled for this discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.