we can chain the methods in the same way. I think this would make typing joy and comfortable (as Rubists proudly say). For example, when you typed >>> range(5), you don’t have to go back, type list(, go to the end, and type ). Just type @list.
Most importantly, you can chain any callable object across any modules, e.g.,
I think the idea is neat, not sure whether the specific notation is crucial.
The pipe operator is very common in UNIX systems, and it would be nice to have it also in Python. It doesn’t have to use @, and it probably can’t use the UNIX notation | (unless the contextual parser can do some magic), but it can probably use |> for example.
The primary objection I can think of is that it would add yet another way to do something (going against the Zen), and the syntax might get messy for anything that isn’t a callable that takes exactly one argument (needing to wrap things in lambdas or create named functions just for this).
Wouldn’t have to do anything to the parser, just add an implementation for __rmatmul__ on the function type
class Function:
...
def __rmatmul__(self, value):
return self(value)
The problem is that almost always the @ (at-sign) operator is considered to have the left operand act on the right operand, so you might want to use the | (pipe) operator instead with __ror__
Silly me… I didn’t know the __matmal__ ops!
I have been wondering why my scintilla in the shell stopped highlighting the decorator from PY35.
Now I got it.
Please let me continue the discussion.
The reason of using @
Let x @f @g => g(f(x)) and regard @ as an operator for f and g (not as a binary operator here).
Then, a product of @f and @g corresponds to the composition of g and f, but in reverse order.
This is the feature of ‘pullback’ that is a fundamental idea in the mapping theory.
The ‘pullback’ is like a mathematically defined synonym of ‘substitution’.
Since @decorator is also a kind of substitution, using the same symbol @ was natural to me.
Pros and cons
@brettcannon, @itamaro,
Yes, the symbol is not crucial. maybe |>, $<, $@, $(…), and whatever. However, we must take care that many symbols can significantly reduce readability. (in this point of view, Ruby is failing, I think).
@EpicWink,
It is Interesting to customize the right-hand-side operator. It really works! But here I don’t want to limit the specific class. I want to extend this feature to the parser of the shell.
Well, I forgot to mention that this topic is intended only for REPL time, not coding time. One may dislike that the syntax of REPL and actual python code is inconsistent. I agree too, but I can also show a few reasonable reasons for accepting this.
The main purpose of REPL is to try it fast and learn what will happen.
We shouldn’t feel stressed about typing. A good REPL will make the user’s learning curve steep.
The history that the interpreter parsed as the actual python code can be recorded and used in the coding time (copy and paste).
P.S.
Thanks for the efforts of all Python core developers and contributors.