[Python-Dev] functools.compose to chain functions together (original) (raw)
Brett Cannon brett at python.org
Fri Aug 14 23:54:30 CEST 2009
- Previous message: [Python-Dev] functools.compose to chain functions together
- Next message: [Python-Dev] functools.compose to chain functions together
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
It would be best to discuss this on comp.lang.python or python-ideas to get general support for the idea before trying to bring this to python-dev in hopes of changing people's minds.
On Fri, Aug 14, 2009 at 11:39, Jason R. Coombs <jaraco at jaraco.com> wrote:
I’d like to express additional interest in python patch 1660179, discussed here:
http://mail.python.org/pipermail/patches/2007-February/021687.html 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 def decregisterfunctionforx(func): # do something with func return func def decalterdocstring(func): # do something to func.doc return func def injectsomedata(data): def decinjectdata(func): func.data = data # this may not be legal, but assume it does something useful return func return decinjectdata # we could use these decorators explicitly throughout our project @decregisterfunctionforx @decalterdocstring @decinjectsomedata(‘foo data 1’) def ourfunc1(params): pass @decregisterfunctionforx @decalterdocstring @decinjectsomedata(‘foo data 2’) def ourfunc2(params): pass 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. def metadecorator(data): return lambda func: decregisterfunctionforx(decalterdocstring(decinjectsomedata(data)(func))) But to me, a compose function is much easier to read and much more consistent with the decorator usage syntax itself. def metadecorator(data): return compose(decregisterfunctionforx, decalterdocstring, decinjectsomedata(data)) The latter implementation seems much more readable and elegant. One doesn’t even need to know the decorator signature to effectively compose metadecorators. 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. Regards, Jason
Python-Dev mailing list Python-Dev at python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/brett%40python.org -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20090814/b6b68e78/attachment-0001.htm>
- Previous message: [Python-Dev] functools.compose to chain functions together
- Next message: [Python-Dev] functools.compose to chain functions together
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]