Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Code formatting question: conditional expression

5 views
Skip to first unread message

John Posner

unread,
Aug 18, 2009, 10:04:36 AM8/18/09
to pytho...@python.org
While refactoring some code, I ran across an opportunity to use a
conditional expression. Original:

if total > P.BASE:
excessblk = Block(total - P.BASE, srccol, carry_button_suppress=True)
else:
excessblk = None

Is there any consensus on how to format a conditional expression that is
too long for one line? How about this:

excessblk = (Block(total - P.BASE, srccol, carry_button_suppress=True)
if total > P.BASE else
None)

The above format separates the values from the if-then-else machinery.
Too many lines? Would it be better to line up "if" and "else"
vertically? ...

excessblk = (Block(total - P.BASE, srccol, carry_button_suppress=True)
if total > P.BASE
else None)

PEP 308 is silent on this topic.

-John

Diez B. Roggisch

unread,
Aug 18, 2009, 10:10:01 AM8/18/09
to
John Posner wrote:

My choice would be

excessblk = None
if total > P.BASE:
excessblk = ...


You don't lose any vertical space, and it's much more readable IMHO.

Diez

Jean-Michel Pichavant

unread,
Aug 18, 2009, 10:32:02 AM8/18/09
to Diez B. Roggisch, pytho...@python.org
+1, I'm using such layout whenever possible.

JM

Steven D'Aprano

unread,
Aug 18, 2009, 10:54:36 AM8/18/09
to
On Tue, 18 Aug 2009 10:04:36 -0400, John Posner wrote:

> While refactoring some code, I ran across an opportunity to use a
> conditional expression. Original:
>
> if total > P.BASE:
> excessblk = Block(total - P.BASE, srccol,
> carry_button_suppress=True)
> else:
> excessblk = None
>
> Is there any consensus on how to format a conditional expression that is
> too long for one line?

Er, that defeats the purpose of using a conditional expression. If it's
too long for one line, leave it as an if...else statement.

> How about this:
>
> excessblk = (Block(total - P.BASE, srccol, carry_button_suppress=True)
> if total > P.BASE else
> None)

If you insist on using the conditional expression, my preference would be:

excessblk = (


Block(total - P.BASE, srccol, carry_button_suppress=True)

if total > P.BASE else None
)


--
Steven

John Posner

unread,
Aug 18, 2009, 10:58:05 AM8/18/09
to pytho...@python.org, Diez B. Roggisch, Jean-Michel Pichavant
>
> My choice would be
>
> excessblk = None
> if total > P.BASE:
> excessblk = ...
>

Diez and Jean-Michel,

Ha! Your suggestion above was my *original* coding. It looks like I'm
evolving backwards!

But doesn't it violate the DRY principle? The token "excessblk" appears
twice instead of once.

Thanks again,
John

Richard Brodie

unread,
Aug 18, 2009, 11:27:52 AM8/18/09
to

"John Posner" <jjpo...@optimum.net> wrote in message
news:mailman.26.12506043...@python.org...

> if total > P.BASE:
> excessblk = Block(total - P.BASE, srccol, carry_button_suppress=True)
> else:
> excessblk = None

I wonder if it is appropriate to replace the None sentinel with one that is an instance
of Block() e.g.

size = total - P.BASE
excessblk = Block(size, srccol, carry_button_suppress=True, empty_block=(size <= 0) )


Jean-Michel Pichavant

unread,
Aug 18, 2009, 11:57:01 AM8/18/09
to John Posner, pytho...@python.org, Diez B. Roggisch
John Posner wrote:
>>
>> My choice would be
>>
>> excessblk = None
>> if total > P.BASE:

>> excessblk = ...
>>
>
> Diez and Jean-Michel,
>
> Ha! Your suggestion above was my *original* coding. It looks like I'm
> evolving backwards!
>
> But doesn't it violate the DRY principle? The token "excessblk"
> appears twice instead of once.
>
> Thanks again,
> John
>
I don't see any problem in that. You are hunting poor readability, not
redundancy, it's up to you to decide of you priorities. I would still
advise that readability should rule your world.

JM


John Posner

unread,
Aug 18, 2009, 1:25:46 PM8/18/09
to pytho...@python.org, Richard Brodie

> I wonder if it is appropriate to replace the None sentinel with one that is an instance
> of Block() e.g.
>
> size = total - P.BASE
> excessblk = Block(size, srccol, carry_button_suppress=True, empty_block=(size <= 0) )
>

In this particular case, Richard, I don't think so. The Block class is
an application-level wrapper for a graphical object. I don't want to
worry about zero-size objects. ("Is this column really empty, or does it
contain one or more zero-size blocks?") If you're interested, take a
look at "BlockHead" at www.jjposner.net.

BTW, from the (admittedly few) responses to my original post, it seems
there's some sentiment that "conditional expressions" are a non-Pythonic
misfeature. Interesting ...

-John

Ethan Furman

unread,
Aug 18, 2009, 2:43:36 PM8/18/09
to pytho...@python.org
John Posner wrote:
>
> BTW, from the (admittedly few) responses to my original post, it seems
> there's some sentiment that "conditional expressions" are a non-Pythonic
> misfeature. Interesting ...
>
> -John
>
>
>

I didn't read it that way. One of the (to me) core points of Pythonic
is readability. A conditional expression on one line is fine -- a
conditional expression on more than one line is less readable than the
standard if-else structure.

~Ethan~

Jan Kaliszewski

unread,
Aug 18, 2009, 3:48:58 PM8/18/09
to Steven D'Aprano, pytho...@python.org
18-08-2009 Steven D'Aprano <st...@remove-this-cybersource.com.au> wrote:

> On Tue, 18 Aug 2009 10:04:36 -0400, John Posner wrote:
>

[snip]


>> How about this:
>>
>> excessblk = (Block(total - P.BASE, srccol, carry_button_suppress=True)
>> if total > P.BASE else
>> None)
>
> If you insist on using the conditional expression, my preference would
> be:
>
> excessblk = (
> Block(total - P.BASE, srccol, carry_button_suppress=True)
> if total > P.BASE else None
> )

Generally I'd prefer:

excessblk = (Block(........) if total > P.BASE
else None)

But it this case first line would be too long, then I'd try using:

excessblk = (Block(total - P.BASE, srccol,
carry_button_suppress=True) if total > P.BASE
else None)

or

excessblk = (Block(total - P.BASE, srccol, carry_button_suppress=True)
if total > P.BASE else None)

I'd use conditional expression only (rather) in situation when the first
expression-part was 'common' and the other (after else) was 'rare'.

Cheers,
*j

--
Jan Kaliszewski (zuo) <z...@chopin.edu.pl>

Ben Finney

unread,
Aug 18, 2009, 5:19:39 PM8/18/09
to
John Posner <jjpo...@optimum.net> writes:

> Is there any consensus on how to format a conditional expression that
> is too long for one line? How about this:
>
> excessblk = (Block(total - P.BASE, srccol, carry_button_suppress=True)
> if total > P.BASE else
> None)
>
> The above format separates the values from the if-then-else machinery.
> Too many lines?

Too much indentation, and worse, indentation that changes based on a
distractingly irrelevant quantity: the length of the first line. I
prefer::

excessblk = (


Block(total - P.BASE, srccol, carry_button_suppress=True)

if total > P.BASE else None)

> Would it be better to line up "if" and "else" vertically? ...

If the expression assigned in the ‘else’ branch were also quite long,
I'd say yes::

excessblk = (


Block(total - P.BASE, srccol, carry_button_suppress=True)

if total > P.BASE
else Block(total, srccol, carry_button_suppress=False))

--
\ “Friendship is born at that moment when one person says to |
`\ another, ‘What! You too? I thought I was the only one!’” —C.S. |
_o__) Lewis |
Ben Finney

Ben Finney

unread,
Aug 18, 2009, 5:24:35 PM8/18/09
to
"Diez B. Roggisch" <de...@nospam.web.de> writes:

> excessblk = None
> if total > P.BASE:
> excessblk = ...
>
> You don't lose any vertical space,

I don't see vertical space as such a scarce resource; we don't have an
imminent newline shortage, to my knowledge. I value it far lower than,
say, local readability.

> and it's much more readable IMHO.

On the basis of local readability, then, I agree that a multi-line
conditional expression is far less quickly comprehensible than a normal
multi-line ‘if’ statement. Assigning the default value of ‘None’ first
(if that's the default) is also good style IMO.

--
\ “Pinky, are you pondering what I'm pondering?” “Wuh, I think |
`\ so, Brain, but wouldn't anything lose its flavor on the bedpost |
_o__) overnight?” —_Pinky and The Brain_ |
Ben Finney

Ben Finney

unread,
Aug 18, 2009, 5:39:13 PM8/18/09
to
John Posner <jjpo...@optimum.net> writes:

> > excessblk = None
> > if total > P.BASE:
> > excessblk = ...
> >

[…]

> But doesn't it violate the DRY principle? The token "excessblk"
> appears twice instead of once.

No, the DRY principle <URL:http://c2.com/cgi/wiki?DontRepeatYourself>
doesn't speak against assigning two *different* values in two
*different* circumstances. That's not duplication, and doesn't risk
contradiction. DRY speaks against repeating the same *meaning* in two
different places.

Because this concept is expressed in natural language and not a
programming language, and because we don't speak in a language under the
same principles we espouse for programming, there are multiple terms
used for this concept :-)

Another term which makes the point slightly more sharply is SPOT: there
should be a Single Point Of Truth for each fact in the overall system.

--
\ “What I have to do is see, at any rate, that I do not lend |
`\ myself to the wrong which I condemn.” —Henry Thoreau, _Civil |
_o__) Disobedience_ |
Ben Finney

John Yeung

unread,
Aug 19, 2009, 12:44:01 AM8/19/09
to
On Aug 18, 1:25 pm, John Posner <jjpos...@optimum.net> wrote:
> BTW, from the (admittedly few) responses to my original
> post, it seems there's some sentiment that "conditional
> expressions" are a non-Pythonic misfeature. Interesting ...

Well, it's not like Guido was especially eager to add it in the first
place.

I personally wouldn't call it a misfeature, but it shares with lambda
the property that it's best used when what you need to say with it is
very short.

John Y.

Diez B. Roggisch

unread,
Aug 19, 2009, 4:52:13 AM8/19/09
to
> BTW, from the (admittedly few) responses to my original post, it seems
> there's some sentiment that "conditional expressions" are a non-Pythonic
> misfeature. Interesting ...

No. I love them. But not if they are so large that they stretch over several
lines (or to many columns).

foo = bar if cond else baz

is more than fine for me. But

foo = I_need_to_do_something_really_complicated_here() if cond else baz

isn't, because one doesn't grasp as easily in one look that we're talking a
ternary operator here.

Diez

Bruno Desthuilliers

unread,
Aug 19, 2009, 9:48:48 AM8/19/09
to
Richard Brodie a �crit :

In which case the last param is possibly redundant - the Block object
knows its size, so it might be able to know by itself if it's empty.

NB : please notice the 'possibly' and 'might' cautions !-)

John Posner

unread,
Aug 19, 2009, 4:36:20 PM8/19/09
to pytho...@python.org, Diez B. Roggisch, psi...@merlin.cs.wisc.edu
Diez wrote:
> No. I love them. But not if they are so large that they stretch over several
> lines (or to many columns).
>
> foo = bar if cond else baz
>
> is more than fine for me. But
>
> foo = I_need_to_do_something_really_complicated_here() if cond else baz
>
> isn't, because one doesn't grasp as easily in one look that we're talking a
> ternary operator here.
>

But the right side of my brain (see Peter Keller's discussion in the
recent "importance of syntax" thread) finds it quite easy to recognize
this as a ternary op:

foo = (I_need_to_do_something_really_complicated_here()
if cond else
baz)

The whitespace below "foo" provides a visual hint, which is confirmed by
the nearby appearance of "if" on the second line. De gustibus non est
disputandum! (or not)

Many thanks to all responders!

-John

Message has been deleted

Aahz

unread,
Aug 21, 2009, 7:38:42 PM8/21/09
to
In article <87ocqch...@benfinney.id.au>,

Ben Finney <ben+p...@benfinney.id.au> wrote:
>"Diez B. Roggisch" <de...@nospam.web.de> writes:
>>
>> excessblk = None
>> if total > P.BASE:
>> excessblk = ...
>>
>> You don't lose any vertical space,
>
>I don't see vertical space as such a scarce resource; we don't have an
>imminent newline shortage, to my knowledge. I value it far lower than,
>say, local readability.

We don't have a newline shortage, but we do have a pixel shortage.
--
Aahz (aa...@pythoncraft.com) <*> http://www.pythoncraft.com/

"I support family values -- Addams family values" --www.nancybuttons.com

Nicola Larosa (tekNico)

unread,
Aug 25, 2009, 12:06:37 PM8/25/09
to
John Posner wrote:
> Is there any consensus on how to format a conditional expression
> that is too long for one line?

Here's my take:

excessblk = Block(total - P.BASE, srccol,
carry_button_suppress=True

) if total > P.BASE else None

--
Nicola Larosa - http://www.tekNico.net/

Nobody knows everything, and nobody is expected to be perfect
in the Ubuntu community (except of course the SABDFL).
- https://launchpad.net/codeofconduct/1.0

Nicola Larosa (tekNico)

unread,
Aug 26, 2009, 1:54:59 AM8/26/09
to
Nicola Larosa wrote:
> Here's my take:
>
>     excessblk = Block(total - P.BASE, srccol,
> carry_button_suppress=True
>         ) if total > P.BASE else None

Oops, it got shortened out: line longer than 72 chars, acceptable in
code, but not in email. I'll try again.

If the first line is too long, I would write it like this:

excessblk = Block(total - P.BASE, srccol,

carry_button_suppress=True) if total > P.BASE else None

If not, like this:

excessblk = Block(total - P.BASE, srccol, cbs=True


) if total > P.BASE else None

If the condition or the last value were too long to make it all fit on
two lines, then it would probably best to revert to a plain "if"
statement.

Ben Finney

unread,
Aug 26, 2009, 2:47:33 AM8/26/09
to
"Nicola Larosa (tekNico)" <nicola...@gmail.com> writes:

> Nicola Larosa wrote:
> > Here's my take:
> >
> >     excessblk = Block(total - P.BASE, srccol,
> > carry_button_suppress=True
> >         ) if total > P.BASE else None
>
> Oops, it got shortened out: line longer than 72 chars, acceptable in
> code, but not in email. I'll try again.

Fine in email; just munged on your behalf (and probably without your
knowledge) by your email service provider. If you don't want that
happening, it's probably best to avoid Google Mail.

--
\ “I wish a robot would get elected president. That way, when he |
`\ came to town, we could all take a shot at him and not feel too |
_o__) bad.” —Jack Handey |
Ben Finney

Jorgen Grahn

unread,
Aug 26, 2009, 9:09:15 AM8/26/09
to
On Wed, 26 Aug 2009 16:47:33 +1000, Ben Finney
<ben+p...@benfinney.id.au> wrote:
> "Nicola Larosa (tekNico)" <nicola...@gmail.com> writes:
>
>> Nicola Larosa wrote:
>> > Here's my take:
>> >
>> > � � excessblk = Block(total - P.BASE, srccol,
>> > carry_button_suppress=True
>> > � � � � ) if total > P.BASE else None
>>
>> Oops, it got shortened out: line longer than 72 chars, acceptable in
>> code, but not in email. I'll try again.
>
> Fine in email; just munged on your behalf (and probably without your
> knowledge) by your email service provider. If you don't want that
> happening, it's probably best to avoid Google Mail.

But this is Usenet (or at least it gets gatewayed there) and there
are such limits here (either by common agreement or by RFC).
Probably not Google's fault.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

0 new messages