Junior — Middle
Have you worked with the concept of closures in your projects?
sobes.tech AI
Answer from AI
Yes, I have worked with closures in Python. A closure is a function that remembers values from its surrounding context, even if that context is no longer active.
An example of using a closure is creating functions with preserved state without using global variables or classes:
def make_multiplier(factor):
def multiplier(number):
return number * factor
return multiplier
times3 = make_multiplier(3)
print(times3(10)) # Outputs 30
Here, multiplier remembers the value of factor from the outer function make_multiplier. This is useful for creating specialized functions on the fly.