tags:

views:

264

answers:

8

Just tooling around for my own amusement, and I want to use a lambda, because I feel like it. Can I replace this function with a lambda?

def isodd(number):
    if (number%2 == 0):
        return False
    else:
        return True

Elementary, yes. But I'm interested to know...

+5  A: 
lambda num: num % 2 != 0
oggy
+3  A: 
isodd = lambda number: number %2 != 0
Moe
+5  A: 

Yes you can:

isodd = lambda x: x % 2 != 0
tkopczuk
+6  A: 

And if you don't really need a function you can replace it even without a lambda. :)

(number % 2 != 0)

by itself is an expression that evaluates to True or False. Or even plainer,

bool(number % 2)

which you can simplify like so:

if number % 2:
    print "Odd!"
else:
    print "Even!"

But if that's readable or not is probably in the eye of the beholder.

Alexander Ljungberg
See, this was the heart of the matter. I was under the impression that both 0 and 1 are True in Python, since they are both ints.
SilentW
http://docs.python.org/library/stdtypes.html -- "The following values are considered false: ... zero of any numeric type, for example, 0"
Ken
I'd still stick it in a function. With modulo 2 I always need a second or two to figure out whether it evaluates true for even or for odd. if isodd(number) is clearer. Little details like this tend to add up to make a real difference.
Ants Aasma
@SilentW: You might have been thinking of Ruby. The only "false" values there are nil and false.
Chuck
@Alexander: Please lose the parentheses in your `if` statement.
John Machin
@John Machin: Sure. I added them to make the idea more clear but you're right, they bug me too.
Alexander Ljungberg
+4  A: 

Others already gave you replies that cover your particular case. In general, however, when you actually need an if-statement, you can use the conditional expression. For example, if you'd have to return strings "False" and "True" rather than boolean values, you could do this:

lambda num: "False" if num%2==0 else "True"

The definition of this expression in Python language reference is as follows:

The expression x if C else y first evaluates C (not x); if C is true, x is evaluated and its value is returned; otherwise, y is evaluated and its value is returned.

Pavel Minaev
+2  A: 

And also don't forget that you can emulate complex conditional sentences with simple short-circuit logic, taking advantage that "and" and "or" return some of their ellements (the last one evaluated)... for example, in this case, supposing you'd want to return something different than True or False

lambda x: x%2 and "Odd" or "Even"
fortran
+1  A: 

isodd = lambda number: (False, True)[number & 1]

John Machin
A: 

Any time you see yourself writing:

if (some condition):
    return True
else:
    return False

you should replace it with a single line:

return (some condition)

Your function then becomes:

def isodd(number):
    return number % 2 != 0

You should be able to see how to get from there to the lambda solution that others have provided.

John Fouhy
Any time you see yourself writing `if (some condition):` or `return (some condition)`, lose the parentheses.
John Machin
Yeah, I know. But I thought the example read better, given that `some condition` is not valid syntax. Maybe I should have used square brackets.
John Fouhy
Try `some_condition`
John Machin