<div dir="ltr"><div><div style="overflow:auto"><div dir="ltr">Dear all,<br><br>The matrix multiplication operator @ is going to be introduced in Python 3.5 and I am thinking about the following idea:<br><br>The semantics of matrix multiplication is the composition of the corresponding linear transformations.<br>A linear transformation is a particular example of a more general concept - functions.<br>The latter are frequently composed with ("wrap") each other. For example:<br><br>plot(real(sqrt(data)))<br><br>However, it is not very readable in case of many wrapping layers. Therefore, it could be useful to employ<br>the matrix multiplication operator @ for indication of function composition. This could be done by such (simplified) decorator:<br><br>class composable:<br><br>    def __init__(self, func):<br>        self.func = func<br><br>    def __call__(self, arg):<br>        return self.func(arg)<br><br>    def __matmul__(self, other):<br>        def composition(*args, **kwargs):<br>            return self.func(other(*args, **kwargs))<br>        return composable(composition)<br><br>I think using such decorator with functions that are going to be deeply wrapped <br>could improve readability.<br>You could compare (note that only the outermost function should be decorated):<br><br>plot(sorted(sqrt(real(data_array)))) vs. (plot @ sorted @ sqrt @ real) (data_array)<br><br>I think the latter is more readable, also compare<br><br>def sunique(lst):<br>    return sorted(list(set(lst)))<br><br>vs. <br><br>sunique = sorted @ list @ set<br><br>Apart from readability, there are following pros of the proposed decorator:<br><br>1. Similar semantics as for matrix multiplication.<br>2. Same symbol for composition as for decorators.<br>3. The symbol @ resembles mathematical notation for function composition: ∘<br><br>I think it could be a good idea to add such a decorator to the stdlib functools module.<br></div></div></div>             </div>