0

I have very basic question. For some reason I'm not able to recall how to do it. How to access the variables defined in the function which is not declared in class. How to access y variable ? And how to pass the value for lambda ? And how to get back the value of c defined inside lambda ?

    def fun ():
        y = 100
        x = lambda c,s,y : c*s*y

Also how to pass the values to lambda from function ?

1

1 Answer 1

2

If you want to use y in lambda then don't specify it as an argument for the lambda:

def fun():
    y = 5
    x = lambda c, s: c*s*y
    return x(2, 3)

>>> fun()
30
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. Is there any way to pass the values to lambda from function ?
@sammy I'm not sure what you are asking. I've just passed y to the lambda. What exactly is what you don't understand?
If my fun function had only lambda then how to pass the values of c,s from fun function ?
@sammy Do you mean def fun(a, b): and then return x(a, b) in the code?
Yes. Similar to that. a and b values should be passed to lambda too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.