Reputation: 1275
The following code:
from functools import partial
def f(a):
print a
g = partial(f, 1)
h = partial(f, 1)
assert(g == h)
raises an assertion error. Why?
Partial returns a callable object with attributes .func, .args, and .keywords. In the example:
g.func == h.func == f
g.args == h.args == (1,)
g.keywords == h.keywords == {}
Shouldn't g==h?
Upvotes: 1
Views: 170
Reputation: 1275
I looked more into the implementation, and the partial object seems to be just an inner function with the attributes mentioned above. Functions never compare equal unless they are the same object. Pity, I have a use case, but there are several work-arounds.
Upvotes: 1