3.12. FuncProg Closure

3.12.1. Rationale

  • Technique by which the data is attached to some code even after end of those other original functions is called as closures

  • When the interpreter detects the dependency of inner nested function on the outer function, it stores or makes sure that the variables in which inner function depends on are available even if the outer function goes away

  • Closures provides some form of data hiding

  • Closures can avoid use of global variables

  • Useful for replacing hard-coded constants

3.12.2. Example

>>> def f(x):
...     def g(y):
...         return x + y
...     return g

3.12.3. Recap

  • Function can access data from outer scope

>>> firstname = 'Mark'
>>> lastname = 'Watney'
>>>
>>> def run():
...     print(f'Hello {firstname} {lastname}')
>>>
>>>
>>> run()
Hello Mark Watney

3.12.4. Nested Function

  • Function inside the function

  • Nested functions can access the variables of the enclosing scope

>>> def run():
...     firstname = 'Mark'
...     lastname = 'Watney'
...     def hello():
...         print(f'Hello {firstname} {lastname}')
...     hello()
>>>
>>>
>>> run()
Hello Mark Watney
>>>
>>> hello()
Traceback (most recent call last):
NameError: name 'hello' is not defined
>>> def run(firstname, lastname):
...     def hello():
...         print(f'Hello {firstname} {lastname}')
...     hello()
>>>
>>>
>>> run('Mark', 'Watney')
Hello Mark Watney

3.12.5. What is closure?

  • Function local variables are stored on the stack (function stack frame)

  • Inner functions have access to outer functions variables (access to outer function stack)

  • In order to that work, you can call inner function only when outer function is running 1

>>> def run():
...     firstname = 'Mark'
...     lastname = 'Watney'
...     def hello():
...         print(f'Hello {firstname} {lastname}')
...     return hello
>>>
>>>
>>> result = run()
>>> result()
Hello Mark Watney

3.12.6. Remove Outer Function

>>> def run():
...     firstname = 'Mark'
...     lastname = 'Watney'
...     def hello():
...         print(f'Hello {firstname} {lastname}')
...     return hello
>>>
>>>
>>> result = run()
>>> del run
>>> result()
Hello Mark Watney

3.12.7. Assignments

Code 3.8. Solution
"""
* Assignment: FuncProg Closure Define
* Complexity: easy
* Lines of code: 4 lines
* Time: 5 min

English:
    1. Define function `check` which takes `func: Callable` as an argument
    2. Define closure function `wrapper` inside `check`
    3. Function `wrapper` takes `*args` and `**kwargs` as arguments
    4. Function `wrapper` returns `None`
    5. Function `check` must return `wrapper: Callable`
    6. Run doctests - all must succeed

Polish:
    1. Zdefiniuj funkcję `check`, która przyjmuje `func: Callable` jako argument
    2. Zdefiniuj funkcję closure `wrapper` wewnątrz `check`
    3. Funkcja `wrapper` przyjmuje `*args` i `**kwargs` jako argumenty
    4. Funkcja `wrapper` zwraca `None`
    5. Funkcja `check` ma zwracać `wrapper: Callable`
    6. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> assert callable(check)
    >>> assert callable(check(lambda x: x))
    >>> result = check(lambda x: x).__call__()
    >>> result is None
    True
"""


Code 3.9. Solution
"""
* Assignment: FuncProg Closure Call
* Complexity: easy
* Lines of code: 9 lines
* Time: 5 min

English:
    1. Define function `check` with parameter `func: Callable`
    2. Define closure function `wrapper` inside `check`
    3. Function `wrapper` takes arbitrary number of positional and keyword arguments
    4. Function `wrapper` prints `hello from wrapper` on the screen
    5. Function `check` must return `wrapper: Callable`
    6. Define function `hello()` which prints `hello from function`
    7. Define `result` with result of calling `check(hello)`
    8. Delete `check` using `del` keyword
    9. Call `result`
    10. Run doctests - all must succeed

Polish:
    1. Zdefiniuj funkcję `check` z parametrem `func: Callable`
    2. Zdefiniuj funkcję closure `wrapper` wewnątrz `check`
    3. Funkcja `wrapper` przyjmuje dowolną ilość argumentów pozycyjnych i nazwanych
    4. Funkcja `wrapper` wypisuje `hello from wrapper`
    5. Funkcja `check` ma zwracać `wrapper: Callable`
    6. Zdefiniuj funkcję `hello()`, która wypisuje `hello from function`
    7. Zdefiniuj zmienną `result`, która jest wynikiem wywołania `check(hello)`
    8. Skasuj `check` za pomocą słowa kluczowego `del`
    9. Wywołaj `result`
    10. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0
    >>> from inspect import isfunction

    >>> assert isfunction(hello)
    >>> assert isfunction(result)
    >>> assert not hasattr(__name__, 'check')

    >>> hello()
    hello from function

    >>> result()
    hello from wrapper

    >>> check()
    Traceback (most recent call last):
    NameError: name 'check' is not defined
"""


3.12.8. References

1

https://youtu.be/t86v3N4OshQ?t=954