Home > Software design >  How to access lambda function's value here?
How to access lambda function's value here?

Time:11-30

def sum_(f, start, end):
    # 1st_value,_,__ = map(f,[start,end]) # how to get 1st value passed on lambda function here ? 
    # print(1st_value start end)
    return
if __name__ == '__main__':
    print(sum_(lambda y: 1.0, 5, 10))) # 1st_value=1.0,2nd_value=5, 3rd_value=10

How can i get 1.0 value from lambda function inside sum_()? I tried map(), but it did not work.

I have gone to other similar questions too, but did not find my answer, so please don't mark it duplicate.

CodePudding user response:

You need to call the lambda function; you have passed the lambda function to the function sum_, but you are not calling the lambda :

>>> def sum_(f, start, end):
        value = f(None) # Pass value of y
        print(start-end value)
>>>> sum_(lambda y: 1.0, 5, 10)
-4.0

A side note, 1st_value that you have in commented part of the code is not a valid variable name.

CodePudding user response:

You must call the lambda function inside sum_(). You can pass whatever value to f since it returns a constant.

def sum_(f, start, end):
    return f(start)   start   end
print(sum_(lambda y: 1.0, 5, 10))
  • Related