Functional style programming in python: what will you talk about if you have an hour on this topic?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Jul 14 06:05:24 CEST 2011


On Thu, 14 Jul 2011 01:32 pm Gregory Ewing wrote:

> Anthony Kong wrote:
> 
>> So I have picked this topic for one of my presentation. It is because
>> functional programming technique is one of my favorite in my bag  of
>> python trick.
> 
> I'm not sure it's a good idea to emphasise functional
> programming too much. Python doesn't really lend itself
> to a heavily functional style. While you *can* write
> Python code that way, it's not idiomatic, and you're
> likely to give beginners a distorted idea of how Python
> is normally written.

I think that's true for *purely* functional code, but functional idioms are
very Pythonic. Iterators and generators, list comprehensions and generator
expressions, itertools, using functions as first-class objects (including
decorators) are all in a functional style. 

Python borrowed list comps from Haskell, just as it borrowed map from Lisp.
If you want to claim that map is "not Pythonic", I won't get into a fight
over it, but list comps certainly are!

I guess it really depends on how you wish to define "functional". To my
mind, even something as simple as "don't use global state, instead pass
arguments to functions as needed" is a Good Trick learned from functional
programming.


-- 
Steven




More information about the Python-list mailing list