Partial Function Application -- Advantages over normal function?

Discussion in 'Python' started by Kurian Thayil, Jul 18, 2011.

  1. 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.
     
    Kurian Thayil, Jul 18, 2011
    #1
    1. Advertising

  2. Kurian Thayil 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.


    It is mostly for functional programming style.

    But one lucky side-effect of the implementation is that partial functions
    *may* sometimes be faster than the alternative written in pure Python,
    provided the original function is written in C:


    from functools import partial
    from operator import add

    def add_one(x):
    return add(1, x) # Like 1+x

    add_two = partial(add, 2)

    from timeit import Timer
    setup = "from __main__ import add_one, add_two"
    t1 = Timer("add_one(42)", setup)
    t2 = Timer("add_two(42)", setup)



    And in action:

    >>> t1.timeit()

    0.7412619590759277
    >>> t2.timeit()

    0.3557558059692383

    So in this example, the partial function is about twice as fast as the one
    written in Python.

    This does not necessarily apply for all functions, but it sometimes is
    useful.


    --
    Steven
     
    Steven D'Aprano, Jul 18, 2011
    #2
    1. Advertising

  3. Kurian Thayil

    woooee Guest

    Partial can be used in a GUI program, like Tkinter, to send arguments
    to functions. There are other ways to do that as well as using
    partial. The following program uses partial to send the color to the
    change_buttons function.
    from Tkinter import *
    from functools import partial

    class App:
    def __init__(self, parent):
    self.my_parent = parent
    self.my_parent.geometry("200x100+10+10")

    self.R = list()
    for ctr, color in enumerate(("Red", "Blue", "Green")):
    btn = Radiobutton(self.my_parent, text=color, value=ctr+1,
    command=partial(self.change_buttons, color))
    btn.grid(row = 2, column = ctr+1)
    btn.deselect()
    self.R.append(btn)
    self.R[0].select()
    self.change_buttons("Red")


    def change_buttons(self, color):
    self.my_parent.configure(bg=color)
    for btn in self.R:
    btn.configure(bg=color)

    if __name__ == "__main__":
    root = Tk()
    root.title ("Color Option")
    app = App(root)
    root.mainloop()
     
    woooee, Jul 18, 2011
    #3
  4. Kurian Thayil

    Terry Reedy Guest

    On 7/18/2011 3:23 PM, woooee wrote:
    > Partial can be used in a GUI program, like Tkinter, to send arguments
    > to functions. There are other ways to do that as well as using
    > partial. The following program uses partial to send the color to the
    > change_buttons function.
    > from Tkinter import *
    > from functools import partial
    >
    > class App:
    > def __init__(self, parent):
    > self.my_parent = parent
    > self.my_parent.geometry("200x100+10+10")
    >
    > self.R = list()
    > for ctr, color in enumerate(("Red", "Blue", "Green")):
    > btn = Radiobutton(self.my_parent, text=color, value=ctr+1,
    > command=partial(self.change_buttons, color))
    > btn.grid(row = 2, column = ctr+1)


    This is a nice illustration. For future reference: enumerate now takes a
    start value as second parameter. Given as 1, you do not need to remember
    to add 1 for each usage.

    for ctr, color in enumerate(("Red", "Blue", "Green"),1):
    btn = Radiobutton(self.my_parent, text=color, value=ctr,
    command=partial(self.change_buttons, color))
    btn.grid(row = 2, column = ctr)

    > btn.deselect()
    > self.R.append(btn)
    > self.R[0].select()
    > self.change_buttons("Red")
    >
    > def change_buttons(self, color):
    > self.my_parent.configure(bg=color)
    > for btn in self.R:
    > btn.configure(bg=color)
    >
    > if __name__ == "__main__":
    > root = Tk()
    > root.title ("Color Option")
    > app = App(root)
    > root.mainloop()



    --
    Terry Jan Reedy
     
    Terry Reedy, Jul 18, 2011
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Dalberg
    Replies:
    4
    Views:
    608
    Karl Seguin
    Dec 8, 2005
  2. Sally Thompson
    Replies:
    10
    Views:
    6,213
    Sally Thompson
    Jun 26, 2004
  3. 79

    JMX advantages over RMI

    79, May 1, 2006, in forum: Java
    Replies:
    3
    Views:
    2,556
    Daniel Dyer
    May 2, 2006
  4. Kamil Burzynski
    Replies:
    17
    Views:
    25,263
    Richard Herring
    Mar 9, 2005
  5. Dave Angel
    Replies:
    8
    Views:
    282
    Ian Kelly
    Jul 19, 2011
Loading...

Share This Page