Newbie question about formatting long conditionals

Discussion in 'Python' started by Barney Frank, Jul 28, 2004.

  1. Barney Frank

    Barney Frank Guest

    I am writing my first application using Python, and there are a
    couple points in code at which I need a fairly complex if-else block.
    I've discovered that you can nest compound conditions in parentheses, but
    I haven't found a way to break a really complex if condition up into
    multiple lines.

    For example, take this little fake snippet:

    #############
    a = 1
    b = 2
    c = 3
    d = 4

    if a == 1 and b < 4 and (c == 2 or d == 4):
    print "Conditions met!"
    #############



    ... if the "if" condition gets too long and unweildy for a single
    line, is there any way to format it along these lines?:

    #############
    a = 1
    b = 2
    c = 3
    d = 4

    if a == 1
    and b < 4
    and (c == 2 or d == 4):
    print "Conditions met!"
    #############
     
    Barney Frank, Jul 28, 2004
    #1
    1. Advertising

  2. Barney Frank wrote:

    > I am writing my first application using Python, and there are a
    > couple points in code at which I need a fairly complex if-else block.
    > I've discovered that you can nest compound conditions in parentheses, but
    > I haven't found a way to break a really complex if condition up into
    > multiple lines.
    >
    > For example, take this little fake snippet:
    >
    > #############
    > a = 1
    > b = 2
    > c = 3
    > d = 4
    >
    > if a == 1 and b < 4 and (c == 2 or d == 4):
    > print "Conditions met!"
    > #############
    >
    >
    >
    > ... if the "if" condition gets too long and unweildy for a single
    > line, is there any way to format it along these lines?:


    Put the whole condition in parentheses:

    if (a == 1 and
    b < 4 and
    etc.):

    HTH,
    James
     
    James Henderson, Jul 28, 2004
    #2
    1. Advertising

  3. > ... if the "if" condition gets too long and unweildy for a single
    > line, is there any way to format it along these lines?:
    >
    > #############
    > a = 1
    > b = 2
    > c = 3
    > d = 4
    >
    > if a == 1
    > and b < 4
    > and (c == 2 or d == 4):
    > print "Conditions met!"
    > #############


    if a == 1 \
    and b < 4 \
    and (c == 2 or d == 4):
    print "Conditions met!"


    if (a == 1
    and b < 4
    and (c == 2 or d == 4)):
    print "Conditions met!"





    --
    Grant Edwards grante Yow! One FISHWICH coming
    at up!!
    visi.com
     
    Grant Edwards, Jul 28, 2004
    #3
  4. Barney Frank

    anton muhin Guest

    Barney Frank wrote:
    > I am writing my first application using Python, and there are a
    > couple points in code at which I need a fairly complex if-else block.
    > I've discovered that you can nest compound conditions in parentheses, but
    > I haven't found a way to break a really complex if condition up into
    > multiple lines.
    >
    > For example, take this little fake snippet:
    >
    > #############
    > a = 1
    > b = 2
    > c = 3
    > d = 4
    >
    > if a == 1 and b < 4 and (c == 2 or d == 4):
    > print "Conditions met!"
    > #############
    >
    >
    >
    > ... if the "if" condition gets too long and unweildy for a single
    > line, is there any way to format it along these lines?:
    >
    > #############
    > a = 1
    > b = 2
    > c = 3
    > d = 4
    >
    > if a == 1
    > and b < 4
    > and (c == 2 or d == 4):
    > print "Conditions met!"
    > #############
    >
    >


    It doesn't answer your question, but still...

    I personally prefer to introduce additional variables (in all the
    languages I write) that _explain_ what precisly the condition mean:

    isTheWorldGoingToBlow = (a == 1) and (b < 4) ....

    if isTheWorldGoingToBlow:

    The reason is simple: usually such a long conditions are too complicated
    for a human to understand without additional hint.

    Just my 5 kopecks :),
    anton.
     
    anton muhin, Jul 29, 2004
    #4
    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. George Marsaglia

    Assigning unsigned long to unsigned long long

    George Marsaglia, Jul 8, 2003, in forum: C Programming
    Replies:
    1
    Views:
    746
    Eric Sosman
    Jul 8, 2003
  2. Daniel Rudy

    unsigned long long int to long double

    Daniel Rudy, Sep 19, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,244
    Peter Shaggy Haywood
    Sep 20, 2005
  3. Mathieu Dutour

    long long and long

    Mathieu Dutour, Jul 17, 2007, in forum: C Programming
    Replies:
    4
    Views:
    515
    santosh
    Jul 24, 2007
  4. Bart C

    Use of Long and Long Long

    Bart C, Jan 9, 2008, in forum: C Programming
    Replies:
    27
    Views:
    852
    Peter Nilsson
    Jan 15, 2008
  5. veryhotsausage
    Replies:
    1
    Views:
    1,898
    veryhotsausage
    Jul 4, 2008
Loading...

Share This Page