Style question for conditional execution

Ed Leafe ed at leafe.com
Wed Nov 24 20:10:23 CET 2010


On Nov 24, 2010, at 1:46 PM, 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()
> 
> 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?

	Readability is key. The first is instantly understandable; the second only if you are familiar with that particular programming construct. Explicit is better than implicit, so I'd go with the first form.


-- Ed Leafe






More information about the Python-list mailing list