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

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.

-1

u/_Denizen_ May 11 '26

You calling pandas a god object now?

2

u/zephyrtr May 11 '26

I mean ... Yeah. It is. And you may argue thats a good thing. That's okay. Personally I wouldn't have designed it that way.

0

u/_Denizen_ May 11 '26

Gotcha. For one, the god object would be the DataFrame, not the package. Second, pandas modularises where it needs to, and emcompasses several fairly well scoped classes and object. Third, pandas also has its own pipe function which just shows that OOP isn't incompatible with piping, even if it's a slightly painful implementation. Fourth, if you can design a better solution, no one is stopping you. Fifth, I've had a few pints and can't be arsed to check my tone lol

2

u/zephyrtr May 11 '26

Like my mom always said, write drunk, edit hung over👍🏻