Quite often we see the following idiom:
def SOME_FUNCTION(...):
...
return SOME_FUNCTION
This is used in a vast majority of functions that return functions.
This might be a far-fetched idea but wouldn’t this code be made clearer with:
@return
def SOME_FUNCTION(...):
...
It wouldn’t save any LOC, I just think it would be a clearer declaration of intent. It would make the code more readable, and would make refactoring easier. return
is already a keyword, so this wouldn’t be a breaking change. But now instead of seeing a function declared and thinking “Where are they going with this?” you’ll think “OK, this is the value the function returns.”
This’ll make a lot of wrapper functions simple to implement, and will clear up a lot of ugly code
def inform_on_calls(callback: Callable):
def actual_decorator(func):
@wraps(foo)
def wrapper(*args, **kwargs):
callback()
foo(*args, **kwargs)
return wrapper
return actual_decorator
Imagine if whenever users had to return a variable, they’d have to first assign it to a variable, and then return it.
With my proposal (which I fully expect to be shot down, I just want to start a discussion on that bothersome idiom), the code would be replaced with:
def inform_on_calls(pre_call: Callable):
@return
def _(func):
@return
@wraps(foo)
def _(*args, **kwargs):
pre_call()
foo()