While going developing OpenERP, I found the following piece of code
'app_date': lambda *a: time.strftime('%Y-%m-%d')
I know what lambda is.My question is why use lambda?Why not just
'app_date': time.strftime('%Y-%m-%d')
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.
'app_date': time.strftime('%Y-%m-%d')will evaluate thetime.strftimeimmediately. By wrapping it in a lambda, its execution is deferred until later (the time when you call the lambda). Roughly speaking, the difference is between “the time when I defined this” and “the time when I am using this”. Look:I allowed some time to elapse in between each
d['a'], d['b'](). Note thatd['a']is always the same: it is the time when I definedd.d['b']is a function.d['b']()(with parentheses) calls the function, which evaluates the time anew on each call, so it is different at each usage.Also, this is nothing special about
lambda. Lambdas are just functions like any other. I could do the same with: