In a recent project of mine I encountered a genuine reason to have a parameter, which accepts a function
, with a default value.
It looks like this, ignoring the context.
def foo(func: Callable[[], None]) -> None:
...
My initial thought was to set the default to lambda: None
. Indicating that the default value is a function that accepts no parameters and returns None
. i.e.
def foo(func: Callable[[], None] = lambda: None) -> None:
...
After some thought I figured lambda: ...
would also get that point across, because I found lambda: None
to look a bit strange. i.e.
def foo(func: Callable[[], None] = lambda: ...) -> None:
...
My question to you is, which of these is better? I am also open to suggestions outside of these two.
Edit: using Callable
to type hint the parameter. The second example in fact returns a non-None value, which does not actually matter since in context the return value is not used--however, that is a noteworthy discrepancy.