Style question for conditional execution

Discussion in 'Python' started by Gerald Britton, Nov 24, 2010.

  1. Writing in Python gives me the luxury of choosing different paradigms
    for similar operations. Lately I've been thinking about a minor
    detail that peaked my interest and am curious what others think:

    Say that I have some function "f" that I will execute if some variable
    "v" evaluates true. Using a classical procedural approach, I might
    write:

    if v:
    f()

    I might, however, think more in a functional-programming direction.
    Then I might write:

    v and f()

    Interestingly, this second expression compiles smaller (though only by
    a little) in both Python 2.6 and 3.1, which I currently have
    installed. If I had thousands of such expressions, I could boast
    about a measurable difference but practically speaking, it is not
    significant.

    What I _am_ interested in, however, is feedback from a style perspective.

    What do the rest of you think about this?

    Have you used the second approach and, if so, what was your motivation?

    Is there a good/bad reason to choose one over the other?

    --
    Gerald Britton
     
    Gerald Britton, Nov 24, 2010
    #1
    1. Advertising

  2. Gerald Britton

    Ian Guest

    On Nov 24, 11:46 am, Gerald Britton <> wrote:
    > Say that I have some function "f" that I will execute if some variable
    > "v" evaluates true.  Using a classical procedural approach, I might
    > write:
    >
    >     if v:
    >         f()
    >
    > I might, however, think more in a functional-programming direction.
    > Then I might write:
    >
    >     v and f()


    The idea that "if" is inherently procedural is mistaken. Functional
    programming emphasizes the use of functions (in the mathematical
    sense) over changes in state. Assuming that f has no side effects,
    either of the above could equally be viewed as functional.

    (Of course, the fact that the return value is simply discarded in both
    of the above cases suggests that f *does* have side effects, in which
    case neither of the above should be viewed as functional.)

    That said, the 'if' version is clearer, so I would nearly always go
    with that. The rare exception would be if I were genuinely interested
    in capturing the value of "v" if it evaluated false. I can't remember
    the last time that was the case.

    Cheers,
    Ian
     
    Ian, Nov 24, 2010
    #2
    1. Advertising

  3. Gerald Britton <> writes:

    > Writing in Python gives me the luxury of choosing different paradigms
    > for similar operations. Lately I've been thinking about a minor
    > detail that peaked my interest and am curious what others think:
    >
    > Say that I have some function "f" that I will execute if some variable
    > "v" evaluates true. Using a classical procedural approach, I might
    > write:
    >
    > if v:
    > f()
    >
    > I might, however, think more in a functional-programming direction.
    > Then I might write:
    >
    > v and f()
    >
    > Interestingly, this second expression compiles smaller (though only by
    > a little) in both Python 2.6 and 3.1, which I currently have
    > installed. If I had thousands of such expressions, I could boast
    > about a measurable difference but practically speaking, it is not
    > significant.
    >
    > What I _am_ interested in, however, is feedback from a style perspective.
    >
    > What do the rest of you think about this?
    >
    > Have you used the second approach and, if so, what was your motivation?
    >
    > Is there a good/bad reason to choose one over the other?


    I would use the if: form every time but it's interesting that the
    "JUMP_FORWARD 0" instruction below doesn't get optimised away.

    If it did, both forms would be the same compiled lengths.

    >>> def g():

    .... if v: f()
    ....
    >>> dis.dis(g2)

    2 0 LOAD_GLOBAL 0 (v)
    3 POP_JUMP_IF_FALSE 16
    6 LOAD_GLOBAL 1 (f)
    9 CALL_FUNCTION 0
    12 POP_TOP
    13 JUMP_FORWARD 0 (to 16)
    >> 16 LOAD_CONST 0 (None)

    19 RETURN_VALUE

    --
    Arnaud
     
    Arnaud Delobelle, Nov 24, 2010
    #3
  4. Gerald Britton

    Paul Rubin Guest

    Gerald Britton <> writes:
    > if v:
    > f()
    >
    > I might, however, think more in a functional-programming direction.
    > Then I might write:
    >
    > v and f()


    Python has conditional expressions. The above would be:

    f() if v else None

    using "and" is bug-prone.
     
    Paul Rubin, Nov 24, 2010
    #4
  5. Gerald Britton

    Asun Friere Guest

    On Nov 25, 7:43 am, Paul Rubin <> wrote:
    > Gerald Britton <> writes:
    > >     if v:
    > >         f()

    >
    > > I might, however, think more in a functional-programming direction.
    > > Then I might write:

    >
    > >     v and f()

    >
    > Python has conditional expressions.  The above would be:
    >
    >     f() if v else None
    >
    > using "and" is bug-prone.


    Using 'and' is indeed bug-prone when used in combination with 'or' to
    achieve a ternary conditional op, as was done the pre PEP308 days, eg
    "val = cond and a or b" because of the possibility that 'a' was itself
    not true, (thus requiring the ugly 'val = (cond and [a] or )[0]').

    But no such bug could occur with this particular idiom. What could
    possibly go wrong here? :)

    That being said, I agree with previous posters that "if cond : fn()"
    wins in terms of readability.
     
    Asun Friere, Nov 25, 2010
    #5
    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. Alec S.
    Replies:
    10
    Views:
    10,325
    Alec S.
    Apr 16, 2005
  2. metaperl
    Replies:
    1
    Views:
    269
    Gabriel Genellina
    Dec 12, 2006
  3. Ken Varn
    Replies:
    0
    Views:
    537
    Ken Varn
    Apr 26, 2004
  4. mklapp

    Conditional execution of Signout

    mklapp, Feb 3, 2004, in forum: ASP .Net Security
    Replies:
    2
    Views:
    177
    Felix Wu [MSFT]
    Feb 4, 2004
  5. petersson
    Replies:
    2
    Views:
    139
    Jürgen Exner
    Jul 22, 2004
Loading...

Share This Page