How shall I define a constant lambda function in python?
I need it to evaluation expressions like
lam (array[1,2,3,4,5])
For now I used
lam = lambda t: 1 + t*0
It works but is it too wasteful?
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.
if you just want to have a function that returns the same thing, no matter what arguments it’s called with, That’s A-OK! You are not in any way obligated to use any of your arguments.
In python,
lambdais a function without a name (and some other, unrelated limitations)If you are going to take the lambda expression and immediately assign its return value to a variable, you are giving a function a name. Don’t do that, just define a regular function. You should reach for lambda when you need to pass a function to another function, and the function you want to use is little, and doesn’t even merit a name (like when it always returns
1). Python has a few such “high order functions” (functions that take other functions as arguments),map,filterandreduceare in the built in namespace.Some of the most used high order functions in python have a special syntax. In the case above, if the function was
map, you can use a list comprehension like so:which reads a little easier and is consistently faster!