I'd like to express additional interest in python patch 1660179, discussed here:
On several occasions, I've had the desire for something like this. I've made due with lambda functions, but as was mentioned, the lambda is clumsy and harder to read than functools.compose would be.
A potentially common use-case is when a library has a multi-decorator use case in which they want to compose a meta decorator out of one or more individual decorators.
Consider the hypothetical library.
# we have three decorators we use commonly
# do something with func
# do something to func.__doc__
func.data = data # this may not be legal, but assume it does something useful
# we could use these decorators explicitly throughout our project
@dec_inject_some_data('foo data 1')
@dec_inject_some_data('foo data 2')
For two functions, that's not too onerous, but if it's used throughout the application, it would be nice to abstract the collection of decorators. One could do this with lambdas.
return lambda func: dec_register_function_for_x(dec_alter_docstring(dec_inject_some_data(data)(f unc)))
But to me, a compose function is much easier to read and much more consistent with the decorator usage syntax itself.
return compose(dec_register_function_for_x, dec_alter_docstring, dec_inject_some_data(data))
The latter implementation seems much more readable and elegant. One doesn't even need to know the decorator signature to effectively compose meta_decorators.
I've heard it said that Python is not a functional language, but if that were really the case, then functools would not exist. In addition to the example described above, I've had multiple occasions where having a general purpose function composition function would have simplified the implementation by providing a basic functional construct. While Python isn't primarily a functional language, it does have some functional constructs, and this is one of the features that makes Python so versatile; one can program functionally, procedurally, or in an object-oriented way, all within the same language.
I admit, I may be a bit biased; my first formal programming course was taught in Scheme.
Nevertheless, I believe functools is the ideal location for a very basic and general capability such as composition.
I realize this patch was rejected, but I'd like to propose reviving the patch and incorporating it into functools.