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


A

Anthony Kong

(My post did not appear in the mailing list, so this is my second try. Apology if it ends up posted twice)

Hi, all,

If you have read my previous posts to the group, you probably have some idea why I asked this question.

I am giving a few presentations on python to my colleagues who are mainly java developers and starting to pick up python at work.

<personal opinion>
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. It also takes me to the rabbit hole of the functional programming world,which is vastly more interesting than the conventional procedural/OO languages.
</personal opinion>

I think I will go through the following items:

itertools module
functools module
concept of currying ('partial')


I would therefore want to ask your input e.g.

Is there any good example to illustrate the concept?
What is the most important features you think I should cover?
What will happen if you overdo it?


Cheers
 
Ad

Advertisements

G

gene heskett

(My post did not appear in the mailing list, so this is my second try.
Apology if it ends up posted twice)
Actually, it did, but gmail, in its infinite wisdom, thinks an echo of your
post to a mailing list is a duplicate, and deletes it. For this exact
reason, I only use gmail for two or 3 of the nearly 40 lists I'm on.

[...]

Cheers, gene
--
"There are four boxes to be used in defense of liberty:
soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author)
He is considered a most graceful speaker who can say nothing in the most
words.
 
J

J Kenneth King

Anthony Kong said:
(My post did not appear in the mailing list, so this is my second try. Apology if it ends up posted twice)

Hi, all,

If you have read my previous posts to the group, you probably have some idea why I asked this question.

I am giving a few presentations on python to my colleagues who are mainly java developers and starting to pick up python at work.

<personal opinion>
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. It also takes me to the rabbit hole of the functional programming world, which is vastly more interesting than the conventional procedural/OO languages.
</personal opinion>

I think I will go through the following items:

itertools module
functools module
concept of currying ('partial')


I would therefore want to ask your input e.g.

Is there any good example to illustrate the concept?
What is the most important features you think I should cover?
What will happen if you overdo it?


Cheers

You might also want to cover gotchas like Python's references.

If you have the time I'd introduce weakref too.
 
T

Terry Reedy

I am giving a few presentations on python to my colleagues who are mainly java developers and starting to pick up python at work.

<personal opinion>
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. It also takes me to the rabbit hole of the functional programming world, which is vastly more interesting than the conventional procedural/OO languages.
</personal opinion>

I think I will go through the following items:

itertools module

The iteration protocol and the notion of iteraables as the common data
exchange format, with associated notions of iterators, generator
functions, and generators, are important features of Python. Not really
functional style, I guess.
functools module
concept of currying ('partial')


I would therefore want to ask your input e.g.

Is there any good example to illustrate the concept?
What is the most important features you think I should cover?

Functions are first-class objects, like everything else.
Use of closures to create functions to be returned.
 
I

Ian Kelly

The iteration protocol and the notion of iteraables as the common data
exchange format, with associated notions of iterators, generator functions,
and generators, are important features of Python. Not really functional
style, I guess.

Xah Lee's assertion to the contrary notwithstanding, it seems to me
that list comprehensions are basically functional in style. They are,
after all, equivalent to "map(f, filter(g, x))".

Iterators, on the other hand, by definition have the property that
each call to iter.next() has the side effect of changing the
iterator's state. Therefore, although they can effectively be used as
a functional building block (e.g. by masking their use with a
comprehension), the iterators themselves are not actually functional.
 
G

Gregory Ewing

Anthony said:
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.
 
Ad

Advertisements

S

Steven D'Aprano

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

Jonathan Hartley

(My post did not appear in the mailing list, so this is my second try. Apology if it ends up posted twice)

Hi, all,

If you have read my previous posts to the group, you probably have some idea why I asked this question.

I am giving a few presentations on python to my colleagues who are mainlyjava developers and starting to pick up python at work.

<personal opinion>
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. It also takes me to the rabbit hole of the functional programming world, which is vastly more interesting than the conventional procedural/OO languages.
</personal opinion>

I think I will go through the following items:

itertools module
functools module
concept of currying ('partial')

I would therefore want to ask your input e.g.

Is there any good example to illustrate the concept?
What is the most important features you think I should cover?
What will happen if you overdo it?

Cheers

I'd think you'd want to at least mention the relatively new
'lru_cache' decorator, for memoizing the return value expensive
functions, providing they are deterministic / pure, etc.
 
Ad

Advertisements

J

Jonathan Hartley

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.

Maybe the talk would work well if not aimed at complete all-round
beginners, but instead aimed at Pythonistas who are interested in
functional programming, or functionistas who are py-curious (I think
the same talk would work well for both groups)
 

Top