0

While learning scopes in python, I come across the below simple code. My doubt is how can I pass the parameter to outer_var and inner_var in the function.

def outerfunc(outer_var):
    def innerfunc(inner_var):
        return outer_var * inner_var
    return innerfunc
0

1 Answer 1

1

Basically, yeah, it's like @jonrsharpe said: it's because of closures.

Closures "close" around the state within a function that can be used later. Every time you run outerfunc(), the code will define a new version of innerfunc() that has access to outer_var that was passed in that time.

Closures are what make parameterized decorators work.

Sign up to request clarification or add additional context in comments.

Comments

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.