Style question for conditional execution

G

Gerald Britton

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

Ian

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
 
A

Arnaud Delobelle

Gerald Britton said:
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.
.... if v: f()
.... 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) 19 RETURN_VALUE
 
P

Paul Rubin

Gerald Britton said:
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.
 
A

Asun Friere

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top