Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'args': 0.05; 'arguments': 0.05; 'function,': 0.07; 'received:verizon.net': 0.07; 'terry': 0.07; '"hello': 0.09; 'freezes': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'subject:Function': 0.09; 'am,': 0.13; 'wrote:': 0.15; 'functools': 0.16; 'parameter:': 0.16; 'parameters,': 0.16; 'reedy': 0.16; 'subject:function': 0.16; 'this:': 0.16; '>>>': 0.16; 'def': 0.16; 'source.': 0.19; 'jan': 0.19; 'seems': 0.20; 'header:In-Reply-To:1': 0.22; 'subject: -- ': 0.25; '(or': 0.25; 'essential': 0.25; 'function': 0.26; 'paul': 0.28; 'import': 0.29; 'generic': 0.29; 'partial': 0.29; 'example': 0.30; 'version': 0.30; 'subject:?': 0.31; 'adds': 0.32; 'it.': 0.33; 'to:addr:python-list': 0.34; 'header:X-Complaints-To:1': 0.34; 'header:User-Agent:1': 0.34; '...': 0.34; 'function.': 0.35; 'useful': 0.37; 'limitation': 0.37; 'some': 0.37; 'but': 0.37; 'received:org': 0.38; 'takes': 0.38; 'subject:: ': 0.38; 'two': 0.38; 'perhaps': 0.39; 'header:Mime-Version:1': 0.39; 'to:addr:python.org': 0.39; 'called': 0.40; 'give': 0.60; 'order': 0.62; 'act': 0.64; 'taking': 0.65; 'subject:over': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Partial Function Application -- Advantages over normal function? Date: Mon, 18 Jul 2011 15:58:51 -0400 References: <201107181543.42229.kurianmthayil@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-74-109-121-73.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.18) Gecko/20110616 Lightning/1.0b2 Thunderbird/3.1.11 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 39 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1311019143 news.xs4all.nl 23922 [2001:888:2000:d::a6]:48191 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:9832 On 7/18/2011 8:24 AM, Paul Woolcock wrote: > 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) Or you can directly write def add2(x): return x + 2 or more generically def makeadder(y) def _add(x): return x+y add2 = makeadder(2) functool.partial is essential a generic version of makeadder in that it also abstract the function/operator. It is useful when one has a function but perhaps not the source. It's limitation is that args are frozen left to right while the above example freezes the right operand. -- Terry Jan Reedy