Daily Python-URL is a website that will look at every day. Today, I saw someone wrote a SimpleWrapper on the ASPN to implement the function of pre-set parameters in advance. His program structure is about the following:
Class SimpleWrapper:
Class methodCall:
DEF __INIT __ (Self, Function, * Params):
Self.function = function
Self.default_params = params
DEF __CALL __ (Self, * params):
Return self.function (* (self.default_params params))
DEF __INIT __ (Self, NS, * params):
Self.Params = params
Self.ns = ns
DEF __GETATTR __ (Self, Attr):
Return SimpleWrapper.MethodCall (Self.ns [attr], * Self.Params);
After reading it, I didn't think it was not bad. I didn't expect it in the comments behind, I actually mentioned ready-made module partial.py, you can download it from Xoltar.org. The implementation of this module is as follows, very simple:
Class Partial (Object):
DEF __INIT __ (* args, ** kW):
Self = args [0]
Self.fn, self.Args, self.kw = (args [1], args [2:], kW)
DEF __CALL __ (Self, * args, ** kW):
IF kw and self.kw:
D = Self.kw.copy ()
D.Update (kW)
Else:
D = kW or self.kw
Return self.fn (* (self.Args args), ** D)
In the later reviews, I also saw an implementation using Closure:
DEF CURRY (FN, * Cargs, ** CKwargs):
DEF CALL_FN (* fargs, ** fkwargs):
D = ckwargs.copy ()
D.Update (fkwargs)
Return Fn (* (cargs fargs), ** D)
Return Call_fn
Which implementation is more efficient? Haven't seen unified comments yet.