Thursday, November 25, 2010


A friend of mine wrote a very interesting function that makes arbitrary methods good for currying when used as a decorator. This is an example he wrote,

import pycurry

def test(aa, bb, cc):
    print aa, bb, cc

f = test(10) # => currying
test(10, 20, 30) # => execute the func

f(40, 50) # => execute the func since sufficient arguments are given
f(200, cc=400) # => You can execute the func like this as well

g = f('aaa') # => curring again
g([10]) # => execute

One example use case is making similar functions that takes some of arguments in common, and some of arguments given previously, like
import pycurry

def myShotMessageSlot(buttonName, toggled):
    print 'Button ' + buttonName + "'s current state is " + str(toggled)


I usually do the same thing using a closure (you can also use partial),
def slotFactory(buttonName):
    def myShotMessageSlot(toggled):
        print 'Button ' + buttonName + "'s current state is " + str(toggled)
    return myShotMessageSlot

but using his pycurry makes it nicer.

By the way I just noticed syntaxhighlighter doesn't work anymore. The JavaScript files were on a free hosting site that has ended its service, I need to put it somewhere else, ahhhh...


Drake said...

really great and easy-to-use module :)

hohehohe2 [at] said...

Hi Drake,
I think so too, and it's fun :)