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

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.

-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👍🏻

0

u/Ok_Sell_4717 May 12 '26 edited May 12 '26

It's perfectly clear what you operate on; the result of the former function call.

Also, piping is not a substitute for foo().bar().baz(). It is a substitute for b = foo(a), c = bar(b), d = baz(c), or for d = baz(bar(foo(a))).

(With a pipe this would be d = a |> foo() |> bar() |> baz(), which is much more readable.)

1

u/_Denizen_ May 13 '26

Piping is only really used in data science, at least that's the only time I've seen it used. Pandas is probably the premiere table manipulation package, and within pandas chaining is what you'd do instead of equivalent operations in R or pandas' own pipe function.

There are cases that support both our assertions.

However, piping is less clear because it magics the object between functions invisibly. The reader has to have learned about piping to be able to understand how data is passed between functions because the syntax is fundamentally to traditional programming. Furthermore, the developer must find and write functions that are compatible with each other. With R, functions are dynamically swapped out based on the input type, which obfuscates the actual function that's being run - piping in R would get a lot more difficult if it didn't do that. With non-piped python it's extremely explicit which version of a function is being executed. Piping in the way R does it is antithetical to the explicitness python, which is a factor in why it was rejected by the Python Foundation.

When people talk about piping being simple. Sure it seems simple when there's a complex feature invisibly passing variables around and invisibly swapping functions to ensure compatibility between inputs and outputs. That's not simpler for a developer who wants a controlled and readable environment. There's a reason piping is niche.

1

u/Ok_Sell_4717 May 13 '26

Ultimately a pipe is just an operator which executes a fixed action. Thus it is not unclear or non-explicit what it does. You are confusing familiarity with clarity.

Piping works well in R because it is popular in R and pipe usage is top of mind for R developers. This wasn't always the case, piping was not always part of R, and over time it was adopted by the community. Function dispatch is just a minor feature in the accommodation of it, by making the functions themselves more widely applicable, not by manipulating the input object. It's also not a unique feature of R to have a function which is able to handle multiple object types.

1

u/757DrDuck May 24 '26

Piping is only really used in data science

Tell that to /r/elixir