Closures In Python

The aim of this page📝is to conceptualize closures and nested scopes, pointing out that there are 2 confusingly unrelated meanings of closures: 1) used in computer science 2) used in math/set theory. This note is about the first meaning encountered in Python. I’ll end with some notes on the 2).

1. REPETITION/FOUNDATION

2. CLOSURE

# >>> lf.__closure__
# (<cell at 0x02201A78: str object at 0x01F64D90>,

3. USAGE: FUNCTION FACTORIES

3.1. EXAMPLE: RAISE NUMBER TO A POWER

SICP, MATH AND CLOSURE CONFUSION

Sussman and Abelson also use the term closure in the 1980s with a second, unrelated meaning: the property of an operator that adds data to a data structure to also be able to add nested data structures. This usage of the term comes from the mathematics usage rather than the prior usage in computer science. The authors consider this overlap in terminology to be “unfortunate.”

Our ability to use tuples as the elements of other tuples provides a new means of combination in our programming language. We call the ability for tuples to nest in this way a closure property of the tuple data type. In general, a method for combining data values satisfies the closure property if the result of the combination can itself be combined using the same method.

--

--

Infrastructure Support Engineer/Technical Writer (snowplow.io) with a passion for Python/writing documentation. More about me: https://pavol.kutaj.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pavol Kutaj

Infrastructure Support Engineer/Technical Writer (snowplow.io) with a passion for Python/writing documentation. More about me: https://pavol.kutaj.com