Style question for conditional execution

Ian ian.g.kelly at gmail.com
Wed Nov 24 14:14:04 EST 2010


On Nov 24, 11:46 am, Gerald Britton <gerald.brit... at gmail.com> 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



More information about the Python-list mailing list