This link has been bookmarked by 7 people . It was first bookmarked on 20 Aug 2006, by Nicolaus Antonius.
-
21 Jun 08
York JongIn functional programming, currying is a way to bind arguments with\na function and wait for the rest of the arguments to show up later.
-
class curry: def __init__(self, fun, *args, **kwargs): self.fun = fun self.pending = args[:] self.kwargs = kwargs.copy() def __call__(self, *args, **kwargs): if kwargs and self.kwargs: kw = self.kwargs.copy() kw.update(kwargs) else: kw = kwargs or self.kwargs return self.fun(*(self.pending + args), **kw)
-
A typical use of curry is to construct callback functions for
GUI operations. When the operation does not really merit a
new function name, curry can be useful in creating these little
functions. This can be the case with commands for buttons, for
example.
self.button = Button(frame, text='A', command=curry(transcript.append, 'A')) -
'Lightweight' subclasses, Nick Perkins, 2001/05/24
This also works very well for creating a sort of lightwieght subclass..
ie. you can curry the constructor of a class to give the illusion of a subclass as follows:
bluewindow = curry(window, bg='blue')
bw = bluewindow()
..of course type(bluewindow) is still type(window), ( not a sub-type )
Additional parameters can still be passed to the curried constructor:
bw2 = bluewindow( title='blah', fg='yellow')
curry is cool, and not just for callbacks!
-
-
11 Apr 08
-
21 Nov 06
-
20 Aug 06
Would you like to comment?
Join Diigo for a free account, or sign in if you are already a member.