473,320 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 1529
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Will Stuyvesant | last post by:
If you know that your source code is going to be used later by others, then I feel that code with the pattern: if some_condition: some_name = some_value else: some_name = other_value is...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
68
by: Lad | last post by:
Is anyone capable of providing Python advantages over PHP if there are any? Cheers, L.
852
by: Mark Tarver | last post by:
How do you compare Python to Lisp? What specific advantages do you think that one has over the other? Note I'm not a Python person and I have no axes to grind here. This is just a question for...
1
by: Cameron Simpson | last post by:
On 23Sep2008 19:52, Neal Becker <ndbecker2@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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.