[Python-Dev] Possible patch for functools partial - Interested?

Rob Cliffe rob.cliffe at btinternet.com
Fri May 7 18:07:55 CEST 2010


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.
If I wanted it to be true, I should have written

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

I find this behaviour natural and expected, both in my example and yours 
(although maybe I've just got used to Python's behaviour).

Similarly, if you wanted p1==p2, why not write

    p1 = partial(operator.add)
    p2 = p1

Maybe I could be persuaded otherwise by a convincing use case, but I rather 
doubt it.
Rob Cliffe

----- Original Message ----- 
From: "VanL" <van.lindberg at gmail.com>
To: <python-dev at python.org>
Sent: Friday, May 07, 2010 3:37 PM
Subject: [Python-Dev] Possible patch for functools partial - Interested?


> Howdy all -
>
> I have an app where I am using functools.partial to bundle up jobs to
> do, where a job is defined as a callable + args. In one case, I wanted
> to keep track of whether I had previously seen a job, so I started
> putting them into a set... only to find out that partials never test
> equal to each other:
>
>>>> import operator
>>>> from functools import partial
>>>> p1 = partial(operator.add)
>>>> p2 = partial(operator.add)
>>>> p1 == p2
> False
>>>> seen = set();seen.add(p1)
>>>> p2 in seen
> False
>
> I created a subclass of functools.partial that provides appropriate
> __eq__ and __hash__ methods, so that this works as expected. I called
> the subclass a Job:
>>>> j1 = Job(operator.add)
>>>> j2 = Job(operator.add)
>>>> j1 == j2
> True
>>>> seen = set();seen.add(j1)
>>>> j2 in seen
> True
>>>> j1 is j2
> False
>
> While I was at it, I also added a nice repr. Would this group be
> interested in a patch, or is this not interesting?
>
> Thanks,
>
> Van
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: 
> http://mail.python.org/mailman/options/python-dev/rob.cliffe%40btinternet.com 



More information about the Python-Dev mailing list