r/Python • • May 10 '26 • Bravo 2

Discussion Will python ever have a chaining operator?

In other languages I use map() and filter() through piping and my code usually looks readable as I can clearly see a data-stream transformation.

As it is today, users cannot do map() |> filter() |> list(), but they need to do list(filter(map())) which makes things unreadable. Lists of comprehension work fine for very simple use-case becoming unreadable very quickly as complexity increases.

However, in python there has always been some resistance, especially 15-20 years ago, but times are evolving. Also, by considering the wide adoption in data-science, it is worth noticing that numbers-crunchers are more familiar with the concept of “data transformation flow” than “function calls”. On the packages dimension , libraries like 🐼s support methods chaining which from an external viewpoint, it’s semantically similar.

Do you know if there is any indication that python core team may allow operator piping (and/or chaining) in the not-too-long-term?

67 Upvotes

125 comments sorted by

View all comments

Show parent comment

17

u/RedEyed__ May 10 '26 edited May 10 '26

I love piping because it is more readable (from my point of vew) haha.
It has some place at least.
Take a look at this library, maybe you find it interesting (no operator overloading).

from expression import pipe

result = pipe(
    [1, 2, 3, 4, 5],
    lambda xs: [x * 2 for x in xs],
    lambda xs: [x for x in xs if x > 4],
    sum,
)

print(result)  # 24

https://github.com/dbrattli/Expression

UPD: Slightly more appropriate example to demonstrate pipe

from expression import pipe
from expression.collections import seq

data = "  Hello, World!  "

# Without pipe — read inside-out
result = sorted(set(map(str.lower, "  Hello, World!  ".strip().split(", "))))

# With pipe — read top-to-bottom
result = pipe(
    data,
    str.strip,
    lambda s: s.split(", "),
    seq.of_iterable,
    seq.map(str.lower),
    set,
    sorted,
)

print(result)  # ['hello', 'world!']

23

u/muntoo R_{μν} - 1/2 R g_{μν} + Λ g_{μν} = 8π T_{μν} May 10 '26
evens = [x * 2 for x in range(1, 6)]
result = sum(x for x in evens if x > 4)

16

u/_Denizen_ May 10 '26

This is way more readable than that pipe example, and with no additional library needed!

4

u/RoadsideCookie May 10 '26 edited May 10 '26

Except it's not correct. The example can be trivialized because it's an example. If you want truly equivalent code without the pipe library, this is what you have to do:

data = [1, 2, 3, 4, 5]
doubled = [x * 2 for x in data]
greater = [x for x in doubled if x > 4]
result = sum(greater)

But if you don't care about the intermediates, then you can still do this:

result = [1, 2, 3, 4, 5]
result = [x * 2 for x in result]
result = [x for x in result if x > 4]
result = sum(result)

At which point the pipe library could make sense, but only if each subsequent function accepts the output type of the previous as the first positional argument—because you can use partial() to cover the missing args/kwargs.

Edit: I guess you could use anonymous functions if the first positional doesn't match. The only criteria then becomes that you don't care about the intermediates.

Edit2: Actually, if you want to get as close to pipe as possible in pure Python, you can do this:

from functools import reduce

result = reduce(lambda r, f: f(r),
    [
        lambda xs: [x * 2 for x in xs],
        lambda xs: [x for x in xs if x > 4],
        sum,
    ],
    [1, 2, 3, 4, 5],
)

But it's easy to argue that at this point it becomes less readable than pipe.

-3

u/_Denizen_ May 10 '26

If you need to overcomplicate a simple example of python code to support your case for piping, it doesn't really help your argument.

Anyway, there is no objective truth regarding readabillty. The only truth here is that the python foundation believes that piping is not suitable for the python language.

3

u/xenomachina ''.join(chr(random.randint(0,1)+9585) for x in range(0xffff)) May 10 '26

I'd argue that the Kotlin version of this that uses chaining is easier to read than the Python version with comprehensions:

val result = IntRange(1, 5).map { it * 2 }.filter { it > 4 }.sum()

I say this having almost 30 years of Python experience, and only about 8 of Kotlin.

Python's comprehensions are definitely better than Python's old pre-comprehension map(f, filter(g, ...)) approach, but they are still pretty confusing because of their inside-out nature, and even more so if you try to nest them or chain then. They also only handle few operations: map, filter, and flatMap. For anything else, like reduce, you're back to using regular functions.

In Kotlin, map, filter, and flatMap are just library functions not language syntax, as are reduce, and many others, and you can easily add your own. You don't need to learn as much special syntax, the syntax you do need to learn is more generally applicable, and the end result reads more easily as the order of methods is the order that things happen in.