r/Python • u/Capable-Mall-2067 • 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
View all comments
Show parent comment
5
u/randomatic May 31 '25
Absolutely. As mentioned, python does not have sum types. You can create a class that looks like a sum type, but the type theory behind it is not algebraic, it's OOP.
Here is how you emulate a sum type using runtime type inspection in python (isinstance) as it's not type-enforced tagged disjunction.
A real algebraic sum type would:
Python's model:
Likely cause of confusion is python's "duck typing", which more accurately should be called "duck runtime checking". Why? Duck typing is not a concept from type theory. It's a dynamic programming idiom with runtime checks, not a principle in the formal foundations of a type system. Duck typing fundamentally undermines the guarantees that static type checking is designed to provide.
Type theory is a mathematical framework to classify and constrain values and programs. It will rigorously define types, with the goal to prove the type system is sound. You really wouldn't do that with python -- it just doesn't make sense.
Why is this important? Well, python isn't going to be a language where you get strong static type checking -- probably ever. Python has a single dynamic type — “object” — and uses runtime checks to differentiate behavior.