I’ve heard that it is possible to substitute an if statement by using a lambda.
Is this possible in Python? If so, how?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Like the others, I’m not sure what you’re asking about, but I’m willing to have a guess.
I sometimes use lambdas in a bit of a hacky way processing results from API calls.
Say for example an element of an API call result should be a numeric string which I’d want as an integer, but occasionally it returns something else.
You could defined a lambda to turn a string into an integer if it is comprised of digits:
This is avoiding an
ifstatement, but not because of thelambda, you could do the same as a function:Update
Less buggy hack, courtesy of Paul McGuire:
i.e. as
int('0')returns an equivalent ofFalsethe lambda might surprise you by returningNonewhen you wanted0