Метод 1: вложенные функции
def outer_function():
def inner_function():
print("This is the inner function.")
print("This is the outer function.")
inner_function()
outer_function()
Выход:
This is the outer function.
This is the inner function.
Метод 2. Возврат функции
def outer_function():
def inner_function():
print("This is the inner function.")
print("This is the outer function.")
return inner_function
func = outer_function()
func()
Выход:
This is the outer function.
This is the inner function.
Метод 3: функция как аргумент
def outer_function(inner_function):
print("This is the outer function.")
inner_function()
def inner_function():
print("This is the inner function.")
outer_function(inner_function)
Выход:
This is the outer function.
This is the inner function.