I want to pass an optional function to another function in python. If no function is passed, then it should just pass over the execution of that (non-existent) function.
The below code works, but I was wondering whether that is a good way. I don’t really like that I have to define the do_nothing
function.
from typing import Callable
def do_nothing():
pass
def some_function(arg1: str, arg2: str, optional_function_to_execute: Callable=do_nothing) -> None:
print(arg1)
optional_function_to_execute()
print(arg2)
def optional_1() -> None:
print("something1")
def optional_2() -> None:
print("something2")
some_function("test1", "test2", optional_1)
some_function("test1", "test2", optional_2)
some_function("test1", "test2")