r/Python • • Sep 29 '16

O'Reilly offers "Functional Programming in Python" by David Mertz as free eBook.

http://www.oreilly.com/programming/free/functional-programming-python.csp?cmp=em-prog-free-na-lgen_safari_20160929
847 Upvotes

69 comments sorted by

View all comments

16

u/DonaldPShimoda Sep 29 '16

I'm excited to check out this book when I've got a chance!

That said, here's a related SO post about "Why isn't Python very good for functional programming?"

18

u/ubernostrum yes, you can have a pony Sep 30 '16

It's worth noting that most "Python is bad for FP" views -- including the one you linked -- really boil down not to "Python is bad at FP", but rather "Python allows this to coexist with other paradigms of programming". Most of the things people then say they'd want to make Python "better" at FP ultimately are "turn Python into a single-paradigm FP language", not "keep Python multi-paradigm, but improve the FP constructs".

14

u/DonaldPShimoda Sep 30 '16

Hmm but some of the fundamentals aren't quite right. Like tail recursion — that's a pretty big deal for FP, and Python is bad at it by design.

Don't get me wrong; I love Python! It's my preferred language, in fact. But because it wasn't built to be functional from the beginning, it will likely never be able to be used as a "good" functional language.

11

u/wting Sep 30 '16

Hmm but some of the fundamentals aren't quite right. Like tail recursion — that's a pretty big deal for FP, and Python is bad at it by design.

Disclaimer: The definition of FP is a ambiguous and thus quite open to interpreation.

I think TCO is a bit of a red herring. Haskell only does it when compiled with -O2, Scala requires function annotations. Clojure defaults to TCO, and most but not all Lisp dialects support TCO in some form. In practice, Haskell generally prefers folds over recursive calls + TCO.

Also you can hack in Python with a decorator.

9

u/DonaldPShimoda Sep 30 '16

Sure, right, I know there's no "correct" definition of what constitutes FP. But if you ask any functional programmer for a list of techniques in their everyday toolbar, I can about guarantee that recursion will be in there.

Maybe TCO isn't handled by default in some of them, but Haskell certainly doesn't have a max recursion depth of 1000 (pretty sure that's Python's... on mobile now or I'd double-check).

And yeah, I've used that decorator setup before. It's definitely neat, and the fact that things like that are so easy to accomplish is part of why I love Python! But it's fundamentally "unpythonic" — Guido would hate it!

To me what makes Python "bad" for functional is that if you were to deliberately write a whole program in functional style, Python would not go out of its way to help you. But I think that's fine — because writing purely functional code would be completely unpythonic! So I think the language is perfectly capable of writing some bits in a functional way, but it's sort of like emulating classes in C with structs: possible, but maybe a little painful.

(Before this goes any further, I just want to explicitly state that I respect your opinion and I'm not trying to argue; I'm just having a discussion because I enjoy talking about these things!)