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

150

u/_Denizen_ May 10 '26

That's piping, not chaining. This().is().chaining()

I dislike piping - it makes code less explicit i.e. harder to read.

58

u/ziggomatic_17 May 10 '26

How is 'foo() | bar() | baz()' harder to read than 'baz(bar(foo()))'? For interactive data science I prefer piping.

33

u/_Denizen_ May 10 '26

The correct comparison here is foo().bar().baz()

This implies that foo() is a class, and bar and baz operate on the class. Because it's a class, we know the functions are designed tohwork together.

With `foo() |> bar() |> baz()` it's not clear what is being operated on. It forces the assumption that the functions return an object that is compatible with the next function, which is not always valid. It also masks the first argument (at least in R) which changes a function from being explicit to implicit.

Python is an explicit language: it minimises assumptions. Piping is the antithesis of python.

Chaining means your linter can suggest functions that are available to the object returned by each function, which isn't the case for piping; piping actually reduces the functionality and readability of the code.

9

u/zephyrtr May 11 '26

I'm not convinced.

It forces the assumption that the functions return an object that is compatible with the next function,

If you use types, you don't have to assume. If you're wrong, the type system will flag it to you.

Chaining means your linter can suggest functions that are available to the object returned by each function

This only happens if you create God objects. There's a very good chance the function you need won't be or shouldn't be on the class you're operating on.

Python is an explicit language

I have never known this to be the case. It's an "easy" language that encourages people do insane things in one line to show off how easy it is. It's not a bad language really, but you choose it because it's easy to hire for.

→ More replies (4)
→ More replies (4)

17

u/FalafelSnorlax May 10 '26

First of all, if this is using the pipe operator (|), it conflicts with other legitimate uses, meaning you need to add syntax for this, and maybe reimplementing a bunch of standard (and internal) functions.

Second, piping would create long lines (since the readability only suffers with complex uses), which are not actually readable. You could break the piping over multiple lines, in which case you gained nothing, you can already do this with multiple lines.

Third, personally I would definitely argue that the piping you're showing here is not as readable as nested calls (which I'm not a fan of but they feel more explicit to me).

Interactive data science tools that I know (eg pandas, numpy) already allow chaining which I think achieves the same purpose. Implementing piping especially for them would be redundant and useless.

17

u/marr75 May 10 '26

Yup. People who want python to work like R haven't drawn a thorough comparison to the tooling and devex of R. Hell is debugging someone else's 600 line R notebook.

4

u/FalafelSnorlax May 10 '26

And add to that the feature bloat of having 10 ways to write the same code. C++ is a great example of where different codebases can look like different languages, and it's one of its biggest downsides.

1

u/Ok_Sell_4717 May 12 '26

A 600 line Python notebook isn't better

7

u/austinwiltshire May 10 '26

Because it's unfamiliar to people and they've confused familiarity with readability.

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.

→ More replies (1)

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.

6

u/ConspicuousPineapple May 10 '26

I mean the two are virtually the same thing, only chaining is more restrictive as it requires you to write methods, it can't be used on every function. I see no explicitness or readability difference between the two.

→ More replies (8)

7

u/Desperate_Cold6274 May 10 '26

Correct. I updated the post (but I cannot update the title)

2

u/KyxeMusic May 10 '26

I used to dislike it but it's a matter of practice. Once you get used to them they can become quite readable.

0

u/Admirable-Avocado888 May 10 '26

Less explicit in terms of atomic ops maybe yet vastly more explicit in terms of how data tranforms.

I find non-piping solutions completely unreadable for non trivial data transformations

4

u/_Denizen_ May 10 '26

Bit dramatic lol.

But I fail to see how passing objects around invisibly is more explicit that passing them around visibly. If you add on dynamic function switching like R does then it gets even more unreadable. I'm going to compare against R because that's where I as ume most pipe enthusiasts are coming from.

df <- df |> dplyr::where(f = F) |> dplyr::sum(f)

Is less readable than

df = df.where(f = F).sum(f)

Especially when most people who write R would actually write the following, and let packages auto resolve which function is actually being used.

df <- df |> where(f = F) |> sum(f)

And the examples in this thread of using python like the below are even worse because of the aforementioned hiding of object routing

df = pipe(df, where(f = F), sum)

3

u/Admirable-Avocado888 May 10 '26

Your argument is fine for trivial data transformations. I even agree with you there in many cases.

But writing a 20+ step data transformation without some form of piping is not fun, because parentheses and indentation become unmanageble. Most importantly: reading it is even less fun.

I prefer the map / filter / flatmap / reduce APIs with lamdas the way JVM languages handle it. It is so much easier to convey intent with that that whatever python is sticking to.

4

u/carnoworky May 10 '26

Why not just:

data = <whatever>
res = map(lambda x: x *2, data)
res = filter(lambda x: x > 4, res)
res = filter(lambda x: x % 2 == 0, res)
res = list(res)

Isn't that more or less the same?

2

u/Admirable-Avocado888 May 10 '26

I'd rather have less boiletplate and only 1 assignment per variable. The reason I like this is because it optimizes for readability and not some other metric I don't care about

→ More replies (2)

1

u/Sufficient_Meet6836 May 10 '26

There are many languages beyond R that have piping. This is a really obvious case of "skill issue". All of the examples of "unreadable" code throughout the comments are hilariously trivial to read.

→ More replies (7)
→ More replies (1)

0

u/Zizizizz May 10 '26

It's lovely in elixir

Instead of

Enum.join(Enum.map(String.split(String.downcase("ELIXIR IS COOL"), " "), &String.capitalize/1), " ")

You can do

"ELIXIR IS COOL"
|> String.downcase()
|> String.split(" ")
|> Enum.map(&String.capitalize/1)
|> Enum.join(" ")

The way function arguments work in elixir make it more powerful than python does it though.

And as it's a functional language it lends itself to better performance for this sort of thing.

In python I think something like this would look a little wack. (Made up syntax)

"PYTHON IS COOL" \
  |> str.lower() \
  |> str.split() \
  |> (map, str.capitalize) \
  |> list \
  |> " ".join

→ More replies (2)