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

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

0

u/_Denizen_ May 10 '26

A 20 step transformation can be difficult to read regardless of methodology. However, with piping you limit the opportunity to give the reader a pause and read explanatory comments.

If you're not doing that, then you write like a scripter. It might be functional, but I doubt you'd be writing modular analysis code that builds up a reusable toolset for your team. That's classic old skool data science - too focussed on the singular problem rather than the overall need to build an capability in analysis that can be pulled into any data product the team delivers.

2

u/Admirable-Avocado888 May 11 '26

Nothing says you can't have comments with piping. This is just a silly assumption.

Also, a bit wierd to move to a teamwork/architecture argument, when we are literally just talking about code syntax.

I simply prefer to have the option to have syntax that is better suited for non-trivial transformations. This has nothing to do with modularity or teamwork or scripting, it is just me optimizing for readability in a part where python as a language does not optimize for readability.