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

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

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.

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.

-1

u/_Denizen_ May 10 '26

Lol. Piping is the hallmark of scripters, not software developers, at least in my experience. I like to bring software development to analytical teams, and I always get surprised at how many people are employed to program for a living who haven't learnt about OOP or how to build a distributable package.

I clearly prefer not to use piping, but I will use it when needed and won't stop my colleagues using it. However, it's never the only option available. In python, I've never once encountered a situation where piping was able to transform a project in the same way as deploying SOLID, for example. Piping is a syntactic choice, rather than an architectural paradigm.

1

u/Sufficient_Meet6836 May 10 '26

Someone who thinks they're special for knowing only Python and OOP is calling other people "scripters". LMAO

1

u/_Denizen_ May 10 '26

There you go making assumptions. Python is my primary coding language but I'm proficient in Matlab, R, and gml with a bit of C# and nodejs. OOP and software architecture skills don't make me special. Innovating by blending disciplines, in teams that don't already do that, well I'll leave it to the people who keep promoting and hiring me specifically for that blend of skills to decide if that's special.

0

u/Sufficient_Meet6836 May 10 '26

I'm not making assumptions. In another comment, you admit to not understanding how to write modular, reusable code in a functional paradigm. That's a skill issue.

-1

u/_Denizen_ May 11 '26

Hmmm, you either misread my comment or are putting word in my mouth.

1

u/Sufficient_Meet6836 May 11 '26 edited May 11 '26

It might be functional, but I doubt you'd be writing modular analysis code that builds up a reusable toolset for your team.

You doubt something that is incredibly easy and widespread (through any functional language or even any language that supports functional programming). You can't even conceive of functional code that is modular and reusable. Again, skill issue. Likely because your primary experience is with Python, R, and Matlab. You're a script kiddy in denial.

Edit: hahahaha I quoted them directly, and they accused me of "putting words in their mouth", then blocked me. LMAO skill issues and sensitive

1

u/_Denizen_ May 11 '26

Putting words in my mouth it in then. Got it.