[Python-Dev] Possible patch for functools partial (original) (raw)

Steve Holden steve at holdenweb.com
Fri May 7 19:57:06 CEST 2010


Steven D'Aprano wrote:

On Sat, 8 May 2010 02:07:55 am Rob Cliffe wrote:

Sorry to grouse, but isn't this maybe being a bit too clever? Using your example, p1 = partial(operator.add) is creating a callable, p1, i.e. a sort of function. Yes I know technically it's not a function, but it behaves very much like one.

Now, if I write def f1(x,y): return x+y def f2(x,y): return x+y I don't expect f1==f2 to be True, even though f1 and f2 behave in exactly the same way, and indeed it is not. I do expect f1==f2, and I'm (mildly) disappointed that they're not. How about

def f1(x, y): return x+y def f2(x, y): return y+x

As you know, there are limits to everything. It seems to me that while pure mathematics can (sometime) easily determine functional equivalence, once you get to code it's a lot harder because there are semantic constraints that don't apply in pure mathematics.

[...] Similarly, if you wanted p1==p2, why not write

p1 = partial(operator.add) p2 = p1 I thought the OP gave a use-case. He's generating "jobs" (partial applied to a callable and arguments), and wanted to avoid duplicated jobs. I think it is reasonable to expect that partial(operator.add, 2) compares equal to partial(operator.add, 2). I don't think he's suggesting it should compare equal to partial(lambda x,y: x+y, 2). Which absence, presumably, also mildly disappoints you?

regards Steve

+0.5 on comparing equal. +1 on a nicer repr for partial objects.

-- Steve Holden +1 571 484 6266 +1 800 494 3119 See PyCon Talks from Atlanta 2010 http://pycon.blip.tv/ Holden Web LLC http://www.holdenweb.com/ UPCOMING EVENTS: http://holdenweb.eventbrite.com/



More information about the Python-Dev mailing list