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 @pycurry.curryfunc 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 @pycurry.curryfunc def myShotMessageSlot(buttonName, toggled): print 'Button ' + buttonName + "'s current state is " + str(toggled) qtToggleButtonAAA.someSignal.connect(myShotMessageSlot('AAA')) qtToggleButtonBBB.someSignal.connect(myShotMessageSlot('BBB')) qtToggleButtonCCC.someSignal.connect(myShotMessageSlot('CCC'))
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 qtToggleButtonAAA.someSignal.connect(slotFactory('AAA')) qtToggleButtonBBB.someSignal.connect(slotFactory('BBB')) qtToggleButtonCCC.someSignal.connect(slotFactory('CCC'))
but using his pycurry makes it nicer.
https://github.com/shomah4a/pycurry
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...
2 comments:
really great and easy-to-use module :)
Hi Drake,
I think so too, and it's fun :)
Post a Comment