Re: Code formatting question: conditional expression
Nicola Larosa (tekNico) nicola.lar...@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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Code formatting question: conditional expression
On Wed, 26 Aug 2009 16:47:33 +1000, Ben Finney ben+pyt...@benfinney.id.au wrote: Nicola Larosa (tekNico) nicola.lar...@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 . -- http://mail.python.org/mailman/listinfo/python-list
Re: Code formatting question: conditional expression
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Code formatting question: conditional expression
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. -- Nicola Larosa - http://www.tekNico.net/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Code formatting question: conditional expression
In article 87ocqchl2k@benfinney.id.au, Ben Finney ben+pyt...@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 (a...@pythoncraft.com) * http://www.pythoncraft.com/ I support family values -- Addams family values --www.nancybuttons.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Code formatting question: conditional expression
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Code formatting question: conditional expression
Richard Brodie a écrit : John Posner jjpos...@optimum.net wrote in message news:mailman.26.1250604346.2854.python-l...@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) ) 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 !-) -- http://mail.python.org/mailman/listinfo/python-list
Re: Code formatting question: conditional expression
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Code formatting question: conditional expression
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? 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) 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Code formatting question: conditional expression
Diez B. Roggisch wrote: 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? 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) 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 +1, I'm using such layout whenever possible. JM -- http://mail.python.org/mailman/listinfo/python-list
Re: Code formatting question: conditional expression
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Code formatting question: conditional expression
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Code formatting question: conditional expression
John Posner jjpos...@optimum.net wrote in message news:mailman.26.1250604346.2854.python-l...@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) ) -- http://mail.python.org/mailman/listinfo/python-list
Re: Code formatting question: conditional expression
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Code formatting question: conditional expression
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Code formatting question: conditional expression
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~ -- http://mail.python.org/mailman/listinfo/python-list
Re: Code formatting question: conditional expression
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Code formatting question: conditional expression
John Posner jjpos...@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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Code formatting question: conditional expression
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Code formatting question: conditional expression
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. -- http://mail.python.org/mailman/listinfo/python-list