r/Python • • May 30 '25

Resource Functional programming concepts that actually work in Python

Been incorporating more functional programming ideas into my Python/R workflow lately - immutability, composition, higher-order functions. Makes debugging way easier when data doesn't change unexpectedly.

Wrote about some practical FP concepts that work well even in non-functional languages: https://borkar.substack.com/p/why-care-about-functional-programming?r=2qg9ny&utm_medium=reddit

Anyone else finding FP useful for data work?

137 Upvotes

41 comments sorted by

View all comments

Show parent comment

1

u/randomatic May 30 '25

Agreed. Functional programming -- at it's core -- means you can reason about code like mathematical functions. Side effects is but one part. Of course you can write code that is "functional-style" (like the OP), but that doesn't mean the language really supports FP thinking, e.g., your composition example.

I think FP goes beyond what you said. I associate FP's with a formally defined core calculus. SML is somewhat of a gold standard here. AFAIK (and I'm sure the internet will correct me if wrong), Python itself has no formally specified type system or operational semantics calculus. (Note: I'm aware of a few researchers who have tried to create small subsets, and MIT uses python to describe formal operational semantics, but that's different than the language itself being based on those semantics.)

Like I said above, I also associate FP with algebraic data types. They correspond to a way of coding, and are clearly defined logical concepts that are orthogonal from OOP objects. OOP typing rules are just not equal to those you'd use to reason about algebraic data types -- it's like trying to use cats to reason about dogs. They both are pets with four legs and a tail, but they're not the same.

I feel (I think consistently) that logic-oriented languages are also different. Someone could decide to try and think logic-oriented in coding, but it wouldn't be natural. Just like here with ADT, just because you can write a prolog interpreter in python and then interpret a prolog program doesn't mean python is prolog. You just think different in logic-oriented languages like datalog and prolog.