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

Discussion in 'Python' started by Anthony Kong, Jul 13, 2011.

  1. Anthony Kong

    Anthony Kong Guest

    (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?

    Anthony Kong, Jul 13, 2011
    1. Advertisements

  2. Anthony Kong

    gene heskett Guest

    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
    gene heskett, Jul 13, 2011
    1. Advertisements

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

    If you have the time I'd introduce weakref too.
    J Kenneth King, Jul 13, 2011
  4. Anthony Kong

    Terry Reedy Guest

    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.
    Functions are first-class objects, like everything else.
    Use of closures to create functions to be returned.
    Terry Reedy, Jul 13, 2011
  5. Anthony Kong

    Ian Kelly Guest

    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 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.
    Ian Kelly, Jul 13, 2011
  6. 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.
    Gregory Ewing, Jul 14, 2011
  7. 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
    Steven D'Aprano, Jul 14, 2011
  8. 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.
    Jonathan Hartley, Jul 14, 2011
  9. 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)
    Jonathan Hartley, Jul 14, 2011
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.