Allow multiline conditions and the like

Discussion in 'Python' started by Yingjie Lan, Nov 1, 2010.

  1. Yingjie Lan

    Yingjie Lan Guest

    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
    Yingjie Lan, Nov 1, 2010
    #1
    1. Advertising

  2. In message <>, Ben Finney wrote:

    > Yingjie Lan <> 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
    Lawrence D'Oliveiro, Nov 1, 2010
    #2
    1. Advertising

  3. 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
    Steven D'Aprano, Nov 1, 2010
    #3
  4. Yingjie Lan

    Chris Rebert Guest

    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
    Chris Rebert, Nov 1, 2010
    #4
  5. Yingjie Lan

    alex23 Guest

    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:
    ...
    alex23, Nov 2, 2010
    #5
  6. Yingjie Lan

    Mark Wooding Guest

    Chris Rebert <> 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]
    Mark Wooding, Nov 4, 2010
    #6
  7. Yingjie Lan

    Chris Rebert Guest

    On Thu, Nov 4, 2010 at 11:09 AM, Mark Wooding <> wrote:
    > Chris Rebert <> 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
    --
    http://blog.rebertia.com
    Chris Rebert, Nov 4, 2010
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ian
    Replies:
    5
    Views:
    6,001
    Kevin Spencer
    Jan 13, 2006
  2. Jeff
    Replies:
    2
    Views:
    944
    clintonG
    Sep 19, 2006
  3. Ryan Taylor
    Replies:
    1
    Views:
    683
    Ryan Taylor
    Sep 9, 2004
  4. dale zhang
    Replies:
    8
    Views:
    420
    Tintin
    Nov 30, 2004
  5. ian
    Replies:
    4
    Views:
    118
    Randy Webb
    Jan 13, 2006
Loading...

Share This Page