Wednesday, April 22, 2009

Functional Programming in Python


Over the past couple years or so I've toyed with functional programming, dabbling in Lisp, Scheme, Erlang, and most recently, Haskell. I've really enjoyed the little bit I've done and have broadened my experience and understanding in the process.

Curious as to what folks have done with Python and functional programming, I recently did a google search I should have run years ago and discovered some community classics. I'm posting them here, in the event that I might spare others such an error in oversight :-)
I've always enjoyed David's writing style, though I've never read his FP articles until now. They were quite enjoyable and have aged well, despite referencing older versions of Python. Andrew's HOWTO provides a wonderful, modern summary.

I make fairly regular use of itertools but have never used the operator module -- though I now look forward to some FP idiomatic Python playtime with it :-) I've never used functools, either.

Enjoy!



5 comments:

Juanjo Conti said...

Thanks for the links (specially the last ones).

Ambush Commander said...

My impression from what Guido has said about functional programming is that it is un-Pythonic, and thus Python has taken active steps to remove functional features from the core of Python (such that you have to import them in). :-/

Duncan McGreggor said...

My pleasure, Juanjo :-)

Duncan McGreggor said...

Ambush Commander:

I whole-heartedly agree: functional programming is not Pythonic. I believe that one should follow best practices when:
- submitting code for inclusion in Python
- submitting code to any established Python-based project
- writing almost all Python code

The exceptions to that rule that I would make would be the following:
- writing code in a decidedly non-Pythonic manner for the purposes of education, elucidation, or entertainment
- writing code that has distinct or drastic benefits over the standard, Pythonic way, for a given and well-defined purpose
- writing code for a project that has, for one reason or another, diverged from the standard, Pythonic way.

I would not take Guido's thoughts about FP in Python as a proclamation of "don't do it", but rather, "if you want to, fine -- just go do it somewhere else." But maybe that's just me ;-)

If FP is important enough to a developer, they should probably learn a language that fully embraces FP and will let them accomplish their goals in an environment that feels the same way. It's culturally appropriate :-)

Btw, my post is aligned along the borders of "education" and "entertainment." These are good resources for someone who is familiar with Python, but hasn't done much with FP. They provide a nice way to ease the transition ;-)

nnis said...

I don't think Guido dislikes functional programming per se, but prefers "flat is better than nested". Functional programming is often very nested, layered and recursive in nature; makes it harder to follow than flat sequential logic. But sometimes the functional approach is beautiful.