python syntax for conditional is unfortunate

N

Neal Becker

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.
 
A

Aaron \Castironpi\ Brady

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>
 
N

namekuseijin

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... ;)
 
N

Neal Becker

Aaron said:
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.
 
A

Aaron \Castironpi\ Brady

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!
 
N

namekuseijin

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
 
R

Roy Smith

Aaron \"Castironpi\" Brady said:
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.
 
B

Bruno Desthuilliers

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...
 
A

Asun Friere

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_case> if <conditions> else
<unusual_case> works 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.
 
P

Pete Forman

Asun Friere said:
> 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 '')
 
A

Asun Friere

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.
 
A

Asun Friere

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.
 
A

Aaron \Castironpi\ Brady

Sorry missing parantheses.  I should test, even for fragments written
out as part of a sentence. %-/


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' ).
 
A

Asun Friere

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.
 
A

Aaron \Castironpi\ Brady

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.

Ah yes. Maybe the order of precedence can undergo a change in the
future. ... Though talk about backwards incompatible. They were two
options if you have a verb with your noun, which would need a
conditional too.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top