In Python, a function is more than a list of commands, as we learnt in  Koan 5. When a function is defined inside another, it becomes a  closure, a sm

Koan 6: The Forgetful Calligrapher - by Vivis Dev

submited by
Style Pass
2025-08-08 07:30:01

In Python, a function is more than a list of commands, as we learnt in Koan 5. When a function is defined inside another, it becomes a closure, a small piece of code that remembers its parent's environment. This memory is powerful, but it's not a picture of the past. It's more like a window. It shows you the world as it is right now, not as it was when the window was first opened. This phenomenon is known as late binding.

Here, execute_commission is the closure. It remembers the item variable from its parent function, make_commission. When we call draw_sun(), it returns "sun" as expected. The closure has a clear, singular memory of its purpose.

Now, let's observe the lazy calligrapher's technique, as described in the koan. He waits until all instructions have been given before beginning his work.

One might expect the output to be "sun," "moon," and then "cloud." However, the code will print "cloud" three times.

Leave a Comment
Related Posts