lambda – Functional python programming and conditionals

lambda – Functional python programming and conditionals

From the link you posted:

FP either discourages or outright disallows statements,
and instead works with the evaluation of expressions

So instead of if-statements, you could use a conditional expression:

def function():
    return (0 if ((A == 0) or (C != 0) or (A > 4)) else

or, (especially useful if there were many different values):

def function():
    return (0 if A == 0 else
            0 if C != 0 else
            0 if A > 4 else

By the way, the linked article proposes

(<cond1> and func1()) or (<cond2> and func2()) or (func3())

as a short-curcuiting equivalent to

if <cond1>:   func1()
elif <cond2>: func2()
else:         func3()

The problem is they are not equivalent! The boolean expression fails to return the right value when <cond1> is Truish but func1() is Falsish (e.g. False or 0 or None). (Or similarly when <cond2> is Truish but func2 is Falsish.)

(<cond1> and func1())

is written with the intention of evaluating to func1() when <cond1> is Truish, but when func1() is Falsish, (<cond1> and func1()) evaluates to False, so the entire expression is passed over and Python goes on to evaluate (<cond2> and func2()) instead of short-circuiting.

So here is a bit of interesting history. In 2005,
Raymond Hettinger found a similar hard-to-find bug in type(z)==types.ComplexType and z.real or z when z = (0+4j) because z.real is Falsish. Motivated by a desire to save us from similar bugs, the idea of using a less error-prone syntax (conditional expressions) was born.

Theres nothing non-functional style in your current code! who said conditionals are not functional anyway? Practically all functional languages have a conditional operator of some sort, for instance the cond special form in Lisp.

Id take issue with the code if it were using the assignment operator, or mutating state in some way (say, appending to a list) but as it is, the function in the question is already in a functional style – there are no state changes.

Perhaps you meant something like this?

return A != 0 and C == 0 and A <= 4 and someOtherFunction()

The above will return False if either A == 0 or C != 0 or A > 4, in all other cases it will return the value of calling someOtherFunction(). And by the way, False can be assumed to evaluate to 0 (for example, 42 + False == 42), so the semantics in the code in the question will be preserved from the callers point of view.

Notice that youre taking the information in the link out of context. Theres absolutely no need to use a lambda for this, the article is only explaining how to get around an inherent limitation of lambdas in Python, which is that you cant return statements inside (like if-elif-else) – only expressions are allowed, but you can fake them with boolean operators. In the context of a normal function by all means, use conditionals.

lambda – Functional python programming and conditionals

Although Peter Norvig is a really great guy, his website is pretty hard to search.

I remember reading about Can I do the equivalent of (test ? result : alternative) in Python? on his site a while back during some research before a functional Python talk.

Im not going to sway you one way or the other in light of my findings, but you should still go and read the section about ternary conditional operators in a functional style.

def if_(test, result, alternative=None):
    If test is true, do result, else alternative. Do means call if callable.
    if test:
        if callable(result): result = result()
        return result
        if callable(alternative): alternative = alternative()
        return alternative

Leave a Reply

Your email address will not be published.