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
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
JM
> 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
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
> 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) )
JM
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
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~
> 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>
> 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
> 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
> > 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
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.
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
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 !-)
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
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
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
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.
> 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
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 .