Reputation: 40982
>>> def accept(d1, d2):
if somefunc(d1,d2) > 32:
h = 1
else:
h = 0
return h
Does Python have a ternary conditional operator? doesn't give a solution for a case one want to return a value. A lambda based solution is preferable.
Upvotes: 1
Views: 71
Reputation: 70602
Or, perhaps trickier,
return int(somefunc(d1, d2) > 32)
Note that int(True) == 1
and int(False) == 0
.
Upvotes: 3
Reputation: 889
Turn into a lambda
(not a explicit function):
accept = lambda d1,d2: 1 if somefunc(d1, d2) > 32 else 0
Upvotes: -1
Reputation: 251368
The "return-value scenario" is no different than any other:
return 1 if somefunc(d1, d2) > 32 else 0
If for some reason you want a lambda:
lambda d1, d2: 1 if somefunc(d1, d2) > 32 else 0
Note that a lambda is no different than a function defined with def
that returns the same thing. Lambdas are just regular functions.
Upvotes: 5