Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Style question for conditional execution

Reply
Thread Tools

Style question for conditional execution

 
 
Gerald Britton
Guest
Posts: n/a
 
      11-24-2010
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
 
Reply With Quote
 
 
 
 
Ian
Guest
Posts: n/a
 
      11-24-2010
On Nov 24, 11:46*am, Gerald Britton <gerald.brit...@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
 
Reply With Quote
 
 
 
 
Arnaud Delobelle
Guest
Posts: n/a
 
      11-24-2010
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
 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      11-24-2010
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.
 
Reply With Quote
 
Asun Friere
Guest
Posts: n/a
 
      11-25-2010
On Nov 25, 7:43*am, Paul Rubin <no.em...@nospam.invalid> wrote:
> Gerald Britton <gerald.brit...@gmail.com> 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 [b])[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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Encapsulating conditional execution based on list membership - how do you do it? metaperl Python 1 12-12-2006 12:37 AM
? ELSE Conditional Comment / Using Conditional Comments Inside Other Tags To Comment Out Attributes Alec S. HTML 10 04-16-2005 02:21 AM
Off topic? Or limited conditional execution... petersson Perl Misc 2 07-22-2004 12:54 PM
Need help with Style conversion from Style object to Style key/value collection. Ken Varn ASP .Net Building Controls 0 04-26-2004 07:06 PM
Conditional execution of Signout mklapp ASP .Net Security 2 02-04-2004 03:10 AM



Advertisments