473,324 Members | 2,179 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,324 software developers and data experts.

Re: python syntax for conditional is unfortunate

On 23Sep2008 19:52, Neal Becker <nd*******@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.

Personally, I think this is deliberate (the wordiness and ordering, not the
reading difficulty). Plenty of people dislike C's ternary b?x:y operator,
presumably for the same reasons.

A good coder will present things clearly. For trivial stuff the one line
form may be fine, and for longer stuff then this:

y = some thing or other \
if x \
else something_else

or:

if x:
y = something or other
else:
y = something_else

should appear. If it's your code, this is up to you. If it's another's,
well anyone can write unreadable code...

Cheers,
--
Cameron Simpson <cs@zip.com.auDoD#743
http://www.cskk.ezoshosting.com/cs/

Principles have no real force except when one is well fed. - Mark Twain
Sep 24 '08 #1
1 918
Cameron Simpson <cs@zip.com.auwrites:
A good coder will present things clearly. For trivial stuff the one
line form may be fine, and for longer stuff then this:

y = some thing or other \
if x \
else something_else
Parentheses are usually more robust for multi-line, where possible:

foo = (bar
if some_condition()
else baz)

The lines can more easily be edited and rearranged without fiddling
with backslashes at the end of every line. (For even more robustness
at the cost of space, separate the parentheses so they're on separate
lines from what they enclose.)

It also addresses the original poster's complaint that there's no
early signal of the compound nature of the expression: the opening
parenthesis signals this.

--
\ “It is far better to grasp the universe as it really is than to |
`\ persist in delusion, however satisfying and reassuring.” —Carl |
_o__) Sagan |
Ben Finney
Sep 24 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

68
by: Lad | last post by:
Is anyone capable of providing Python advantages over PHP if there are any? Cheers, L.
13
by: Neal Becker | last post by:
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...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shllpp 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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.