

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Closures
incomplete
2: Closure Review
incomplete
3: Closure Practice
incomplete
4: Closure Practice
incomplete
This lesson's interactive features are locked, please to keep using them
The whole point of a closure is that it's stateful. It's a function that "remembers" the values from the enclosing scope even after the enclosing scope has finished executing.
It's as if you're saving the state of a function at a particular point in time, and then you can use and update that state later on.
from collections.abc import Callable
def concatter() -> Callable[[str], str]:
doc: str = ""
def inner_func(word: str) -> str:
# "nonlocal" tells Python to use the doc
# variable from the enclosing scope
nonlocal doc
doc += word + " "
return doc
return inner_func
harry_potter_aggregator: Callable[[str], str] = concatter()
harry_potter_aggregator("Mr.")
harry_potter_aggregator("and")
harry_potter_aggregator("Mrs.")
harry_potter_aggregator("Dursley")
harry_potter_aggregator("of")
harry_potter_aggregator("number")
harry_potter_aggregator("four,")
harry_potter_aggregator("Privet")
print(harry_potter_aggregator("Drive"))
# Mr. and Mrs. Dursley of number four, Privet Drive
That means that in many cases, closures are not pure functions. They can mutate state outside of their scope and have side effects.