11.14. Functional Namespace
11.14.1. About
Functions provide namespaces
Only code inside that namespace can access it's locals
In Python, a function is a namespace, which means that it is a container for variables and other objects. The variables and objects defined within a function are only accessible within that function's namespace, and cannot be accessed from outside the function.
When a function is called, a new namespace is created for it. This namespace contains all the local variables and objects defined within the function, as well as any arguments passed to the function. The namespace is destroyed when the function returns.
Using functions as namespaces helps to keep code modular and organized. It also helps to prevent naming conflicts between different parts of a program.
11.14.2. Example
Here's an example of using a function as a namespace in Python:
>>> def my_function():
... x = 10
... y = 20
... print(x + y)
>>>
>>> my_function()
30
>>> print(x)
Traceback (most recent call last):
NameError: name 'x' is not defined
In this example, the my_function()
function defines two variables
(x
and y
) within its namespace. These variables are only accessible
within the function. When the function is called, it prints the sum of x
and y
. When the function returns, the namespace is destroyed, and the
variables are no longer accessible.
If we try to access the x
variable outside the function, we get a
NameError
because the variable is not defined in the global namespace.
11.14.3. Variables Inside Function
Variables inside function
>>> def run():
... name = 'Mark'
11.14.4. Functions Inside Function
Functions inside function
>>> def run():
... def say_hello():
... return 'Hello'
11.14.5. Functions and Variables
>>> def run():
... name = 'Mark'
... def say_hello():
... return 'Hello'
11.14.6. Call
Function returns
None
if there is no return statement
>>> def run():
... name = 'Mark'
... def say_hello():
... return 'Hello'
>>>
>>>
>>> run()
11.14.7. Return Variable
>>> def run():
... name = 'Mark'
... def say_hello():
... return 'Hello'
... return name
>>>
>>>
>>> run()
'Mark'
11.14.8. Return Function Results
>>> def run():
... name = 'Mark'
... def say_hello():
... return 'Hello'
... return say_hello()
>>>
>>>
>>> run()
'Hello'
11.14.9. Return Function
>>> def run():
... name = 'Mark'
... def say_hello():
... return 'Hello'
... return say_hello
>>>
>>>
>>> run()
<function run.<locals>.say_hello at 0x...>
11.14.10. Locals
>>> def run():
... name = 'Mark'
... def say_hello():
... return 'Hello'
... return locals()
>>>
>>>
>>> run()
{'name': 'Mark', 'say_hello': <function run.<locals>.say_hello at 0x...>}
11.14.11. Assignments
# %% About
# - Name: Functional Closure Define
# - Difficulty: easy
# - Lines: 4
# - Minutes: 3
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% English
# 1. Define function `check` with `func: Callable` as a parameter
# 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`, z `func: Callable` jako parametr
# 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ść
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert callable(check)
>>> assert callable(check(lambda:...))
>>> result = check(lambda:...).__call__()
>>> result is None
True
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% Imports
# %% Types
from typing import Callable
check: Callable[[Callable], Callable]
# %% Data
# %% Result
def check():
...