Prefix shorthand for sending args by using `->`

We often see that in a function call, another function call is nested, such as func1(some_arguments, func2(func3(value), some_another_arguments)). Even with indentation and highlighting help, such code is still a little hard to read, because we need to start reading from the innermost parentheses and work our way out, unlike the usual left-to-right reading.

I have got an idea for ->, with which we can use a prefix shorthand for a args. So like this:

expression -> callable(pass)
expression -> callable
callable(expression)
# the 3 above is equivalent

expression -> callable(1, pass, 3, kwarg1=4)
callable(1, expression, 3, kwarg1=4)
# the 2 above is equivalent

It basically puts the left expression to the position of the right pass, but the expression is evaluated before anything on the right of the arrow. expression -> callable is a grammar sugar for expression -> callable(pass)

With arrow calls we can do something -> print or something -> logger.info

I do recognize the problem with the brackets.

I recall that there was a time when I could compose functions with @ such that
f@g(x) == f(g(x))
which helped, though not with the problem of reading right-to-left. But I’ve never been able to reproduce it.


callable(1, pass, 3, kwarg1=4)

That’s an intriguing idea for the creation of partial functions, and there are indeed situations where I could see it being useful.


Personally I would always prefer

tmp = something
print(tmp)

or straight print(something) over something -> print.

Perhaps you’ll find other people who disagree :slight_smile:

Stuff like this has been suggested a few times under names like “pipe” or “chain”. I would suggest you research this a bit and try to find out what the general objections to this idea are and try to argue against them.

6 Likes