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&#39;s minds.<br><br><div class="gmail_quote">On Fri, Aug 14, 2009 at 11:39, Jason R. Coombs <span dir="ltr">&lt;<a href="mailto:jaraco@jaraco.com">jaraco@jaraco.com</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">








<div lang="EN-US" link="blue" vlink="purple">

<div>

<p>I’d like to express additional interest in python
patch 1660179, discussed here:</p>

<p> </p>

<p><a href="http://mail.python.org/pipermail/patches/2007-February/021687.html" target="_blank">http://mail.python.org/pipermail/patches/2007-February/021687.html</a></p>

<p> </p>

<p>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.</p>

<p> </p>

<p>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.</p>

<p> </p>

<p>Consider the hypothetical library.</p>

<p> </p>

<p># we have three decorators we use commonly</p>

<p>def dec_register_function_for_x(func):</p>

<p>                # do something with func</p>

<p>                return func</p>

<p> </p>

<p>def dec_alter_docstring(func):</p>

<p>                # do something to func.__doc__</p>

<p>                return func</p>

<p> </p>

<p>def inject_some_data(data):</p>

<p>                def dec_inject_data(func):</p>

<p>                                func.data = data # this may
not be legal, but assume it does something useful</p>

<p>                                return func</p>

<p>                return dec_inject_data</p>

<p> </p>

<p># we could use these decorators explicitly throughout our
project</p>

<p>@dec_register_function_for_x</p>

<p>@dec_alter_docstring</p>

<p>@dec_inject_some_data(‘foo data 1’)</p>

<p>def our_func_1(params):</p>

<p>                pass</p>

<p> </p>

<p>@dec_register_function_for_x</p>

<p>@dec_alter_docstring</p>

<p>@dec_inject_some_data(‘foo data 2’)</p>

<p>def our_func_2(params):</p>

<p>                pass</p>

<p> </p>

<p>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.</p>

<p> </p>

<p>def meta_decorator(data):</p>

<p>                return lambda func: dec_register_function_for_x(dec_alter_docstring(dec_inject_some_data(data)(func)))</p>

<p> </p>

<p>But to me, a compose function is much easier to read and much
more consistent with the decorator usage syntax itself.</p>

<p> </p>

<p>def meta_decorator(data):</p>

<p style="text-indent:.5in">return compose(dec_register_function_for_x,
dec_alter_docstring, dec_inject_some_data(data))</p>

<p> </p>

<p>The latter implementation seems much more readable and
elegant.  One doesn’t even need to know the decorator signature to
effectively compose meta_decorators.</p>

<p> </p>

<p>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.</p>

<p> </p>

<p>I admit, I may be a bit biased; my first formal programming
course was taught in Scheme.</p>

<p> </p>

<p>Nevertheless, I believe functools is the ideal location for
a very basic and general capability such as composition.</p>

<p> </p>

<p>I realize this patch was rejected, but I’d like to
propose reviving the patch and incorporating it into functools.</p>

<p> </p>

<p>Regards,</p>

<p>Jason</p>

</div>

</div>


<br>_______________________________________________<br>
Python-Dev mailing list<br>
<a href="mailto:Python-Dev@python.org">Python-Dev@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/python-dev" target="_blank">http://mail.python.org/mailman/listinfo/python-dev</a><br>
Unsubscribe: <a href="http://mail.python.org/mailman/options/python-dev/brett%40python.org" target="_blank">http://mail.python.org/mailman/options/python-dev/brett%40python.org</a><br>
<br></blockquote></div><br>