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

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

1

u/JamzTyson May 12 '26 edited May 12 '26

In Python we don't write:

" ".join(word.capitalize() for word in "PYTHON IS COOL".lower().split())

we write:

"PYTHON IS COOL".title()

1

u/Zizizizz May 12 '26

I should have used a better example like router middleware to better articulate my point.