FAQ
Hi,

This is a mini-proposal I piggy-tailed in the other topic:

Allow the conditions in the if-, elif-, while-, for-, and
with-clauses to span multiple lines without using a backlalsh
at the end of a line,
just like when you specify literal lists, tuples, dicts, etc.
across multiple lines (similar to comprehensions too).

My reasons:

because they all must end with a required colon ':',
so nobody will mistake it.

also, if we don't allow it, people just have to use
parenthesis around the expressions to make that happen.


Just a half-baked idea, appreciate all comments.

Yingjie

Search Discussions

  • Ben Finney at Nov 1, 2010 at 7:13 am

    Yingjie Lan <lanyjie at yahoo.com> writes:

    Allow the conditions in the if-, elif-, while-, for-, and with-clauses
    to span multiple lines without using a backlalsh at the end of a line,
    You can already do this with any expression: use parentheses.


    Yingjie Lan <lanyjie at yahoo.com> writes:
    I would like to have comments after the line continuation backslash.
    I have almost never needed a line continuation backslash; I consider
    them a code smell.
    if a > 0 \ #comments for this condition
    and b > 0:
    #do something here
    if (a > 0 # Comments for this condition
    and b > 0):
    # Do something here

    --
    \ ?Alternative explanations are always welcome in science, if |
    `\ they are better and explain more. Alternative explanations that |
    _o__) explain nothing are not welcome.? ?Victor J. Stenger, 2001-11-05 |
    Ben Finney
  • Lawrence D'Oliveiro at Nov 1, 2010 at 7:16 am

    In message <874oc1ldo6.fsf at benfinney.id.au>, Ben Finney wrote:

    Yingjie Lan <lanyjie at yahoo.com> writes:
    Allow the conditions in the if-, elif-, while-, for-, and with-clauses
    to span multiple lines without using a backlalsh at the end of a line,
    You can already do this with any expression: use parentheses.
    It?s easy enough to do things like this:

    if (
    TheMesh.vertices[OtherVertex].select
    and
    OtherVertex != ThatVertex
    and
    OtherVertex != ThisLine[-2]
    and
    OtherVertex != ThisVertex
    ) :
    ...
    #end if
  • Steven D'Aprano at Nov 1, 2010 at 7:51 am

    On Sun, 31 Oct 2010 23:02:21 -0700, Yingjie Lan wrote:

    Hi,

    This is a mini-proposal I piggy-tailed in the other topic:

    Allow the conditions in the if-, elif-, while-, for-, and with-clauses
    to span multiple lines [...]
    also, if we don't allow it, people just have to use
    parenthesis around the expressions to make that happen.
    You say that like it's a bad thing.

    That is kind of like saying "We should allow people to speed through red
    traffic lights, because if we don't, they'll just wait for the light to
    turn green!". Er, yes, and the problem is?

    If you need a multi-line conditional, wrap it in parentheses.



    --
    Steven
  • Chris Rebert at Nov 1, 2010 at 8:22 am

    On Mon, Nov 1, 2010 at 12:51 AM, Steven D'Aprano wrote:
    On Sun, 31 Oct 2010 23:02:21 -0700, Yingjie Lan wrote:
    Hi,

    This is a mini-proposal I piggy-tailed in the other topic:

    Allow the conditions in the if-, elif-, while-, for-, and with-clauses
    to span multiple lines [...]
    ? ?also, if we don't allow it, people just have to use
    parenthesis around the expressions to make that happen.
    You say that like it's a bad thing.

    That is kind of like saying "We should allow people to speed through red
    traffic lights, because if we don't, they'll just wait for the light to
    turn green!". Er, yes, and the problem is?

    If you need a multi-line conditional, wrap it in parentheses.
    Or, if possible, refactor the conditional into a function (call) so
    it's no longer multiline in the first place.

    Cheers,
    Chris
  • Alex23 at Nov 2, 2010 at 2:00 am

    Chris Rebert wrote:
    Or, if possible, refactor the conditional into a function (call) so
    it's no longer multiline in the first place.
    Or even simpler, assign the condition result to a variable:

    a_b_positive = a > 0 and b > 0
    if a_b_positive:
    ...
  • Mark Wooding at Nov 4, 2010 at 6:09 pm

    Chris Rebert <clp2 at rebertia.com> writes:

    Or, if possible, refactor the conditional into a function (call) so
    it's no longer multiline in the first place.
    No! This /increases/ cognitive load for readers, because they have to
    deal with the indirection through the name. If you actually use the
    function multiple times, the mental overhead of forming the abstraction
    and associating it with the function name is shared across the various
    call sites and it's probably worth it. If it's only called once, leave
    it inline.

    -- [mdw]
  • Chris Rebert at Nov 4, 2010 at 9:00 pm

    On Thu, Nov 4, 2010 at 11:09 AM, Mark Wooding wrote:
    Chris Rebert <clp2 at rebertia.com> writes:
    Or, if possible, refactor the conditional into a function (call) so
    it's no longer multiline in the first place.
    No! ?This /increases/ cognitive load for readers, because they have to
    deal with the indirection through the name.
    If it's well-named, then the reader can delay having to read the definition.
    If you actually use the
    function multiple times, the mental overhead of forming the abstraction
    and associating it with the function name is shared across the various
    call sites and it's probably worth it. ?If it's only called once, leave
    it inline.
    I'd say it's a judgment call. If the condition is sufficiently
    complicated and can be well-named, then I see justifying refactoring
    it into a function even if it's only used once.
    However, this is admittedly not a common situation.

    Cheers,
    Chris

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedNov 1, '10 at 6:02a
activeNov 4, '10 at 9:00p
posts8
users7
websitepython.org

People

Translate

site design / logo © 2022 Grokbase