Boolean tests [was Re: Attack a sacred Python Cow]

Carl Banks pavlovevidence at gmail.com
Fri Aug 1 00:19:21 EDT 2008


On Jul 31, 11:44 pm, Carl Banks <pavlovevide... at gmail.com> wrote:
[snip excellent explanation of why it's hard to for "if x" to be
extensively polymorphic]


By the way, one thing I forgot to mention is Matt Fitzgibbons' filter
example.

As I said, it's hard to write code that works for both numeric and
container types because they share so few methods.  However, sometimes
you don't know ahead of time what methods are used!  When you're doing
functional programming you might pass in a method that calls the
appropriate method, like so:

def apply_if_true(func,x):
    if x:
        func(x)

The function f could use methods appropriate for ints when x is an
int, and for lists when x is an int.

I did downplay this, because frankly, not many programmers use
functional methods this extensively.  But one should also note that
you could pass in the test as well:

def apply_if_condition(func,cond,x):
    if cond(x):
        func(x)

Thus this usage of "if x" arguably could be considered "replaceable
with a simple explicit test".  But in the interests of harmony I
didn't push the point, and it really would have been stretching the
spirit of what I was trying to prove.


Carl Banks



More information about the Python-list mailing list