Partial Function Application -- Advantages over normal function?

Paul Woolcock pwoolcoc at gmail.com
Mon Jul 18 14:24:17 CEST 2011


Partial function application (or "currying") is the act of taking a function
with two or more parameters, and applying some of the arguments in order to
make a new function.  The "hello world" example for this seems to be this:

Let's say you have a function called `add`, that takes two parameters:

    >>> def add(left, right):
    ...     return left + right

Now let's say you want a function that always adds 2 to a number you give
it.  You can use partial function application to do this:

    >>> from functools import partial
    >>> add2 = partial(add, right=2)

Now, you have a new function, `add2`, that takes one parameter:

    >>> add2(4)
      6


---
Paul Woolcock
pwoolcoc at gmail.com





On Mon, Jul 18, 2011 at 6:13 AM, Kurian Thayil <kurianmthayil at gmail.com>
wrote:
>
> Hi,
>
> I am a newbie in python and would like to learn GUI programming. I would
like
> to know what exactly is Partial Function Applicaton (functool.partial())?
Or
> how is it advantageous compared to normal functions? Or is there any
> advantange? Thanks in advance.
>
> Regards,
> Kurian Thayil.
> --
> http://mail.python.org/mailman/listinfo/python-list
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110718/bd4a6d0b/attachment.html>


More information about the Python-list mailing list