Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 8 of 10 · Functions and Lambda Expressions
Interview question

What is a closure? Closure क्या है?

Answer
def make_multiplier(n):
    def multiplier(x):
        return x * n  # Captures n
    return multiplier

multiply_by_2 = make_multiplier(2)
print(multiply_by_2(5))  # 10

multiply_by_3 = make_multiplier(3)
print(multiply_by_3(5))  # 15
def outer():
    x = 10
    def inner():
        return x  # Captures x
    return inner

func = outer()
print(func())  # 10

Was this answer clear?