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!)

2

u/kankyo Sep 30 '16

Clojure has a fancy go to/continue, not TCO

4

u/wting Sep 30 '16

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".

I think this is a really valuable insight. The reality is when designing a language you can encourage pardigms by making it easier / more difficult. At the end of the day, GvR doesn't like FP and he will not make it easier to do in Python.

In fact, lambdas and functools only made it into Python because of an early contributor during Python 1.x or 2.2 days and people used it. Now GvR's stuck with it for backwards compatibility purposes.

I code FP-style Python when it's appropriate, but I wish people wouldn't kid themselves and pretend Python's a first-class functional language (or will ever be).

4

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

Some of the suggestions in the post aren't bad, but would change the character of the language.

Others -- like automatic currying -- probably can't be integrated into Python in any useful way (automatic currying and variadic functions don't play super nice together, and if forced to choose one or the other I'd keep variadic functions easy and make currying something you have to do explicitly).

7

u/DavidMertz Sep 30 '16

I quite like http://toolz.readthedocs.io/en/latest/curry.html. I didn't know about this library when I wrote the book, but it is very good.

1

u/hanpari Sep 30 '16

And what about pyrsistent? I believe toolz and pyrsistent are quite fundamental for FP in Python :) I thought both of them were included in your book.

Maybe I would add attrs https://attrs.readthedocs.io/en/stable/

Even it is not quite FP but can help o reduce codebase significantly.

BTW, your book is great, thank you.