Eduardo
Eduardo

Reputation: 1275

Why do identical partials compare as not equal in Python (2.7.11)?

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

Answers (1)

Eduardo
Eduardo

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

Related Questions