msg215363 - (view) |
Author: Thomas Heller (theller) * |
Date: 2014-04-02 07:57 |
I think that 'equivalent' functools.partial objects should compare equal, so the following snippet SHOULD print True:
>>> import functools
>>> f = lambda x: x
>>> a = functools.partial(f, 42)
>>> b = functools.partial(f, 42)
>>> a == b
False
>>>
|
msg215369 - (view) |
Author: Raymond Hettinger (rhettinger) * |
Date: 2014-04-02 09:56 |
Even equivalent functions don't compare as equal:
>>> (lambda x: x*x) == (lambda x: x*x)
False
|
msg215374 - (view) |
Author: Thomas Heller (theller) * |
Date: 2014-04-02 10:39 |
Sure. I'm not sure 'equivalent' is the word I should have used.
I suggest (and I would have expected) that partial instances (a, b) should compare equal when
a.func == b.func \
and a.args == b.args \
and a.keywords == b.keywords
|
msg215377 - (view) |
Author: Eryk Sun (eryksun) * |
Date: 2014-04-02 12:11 |
Function equality is based on identity, as inherited from object. But there's a precedent for this idea in method_richcompare, which compares the method's __func__ and __self__:
http://hg.python.org/cpython/file/04f714765c13/Objects/classobject.c#l208
|
msg215403 - (view) |
Author: R. David Murray (r.david.murray) * |
Date: 2014-04-02 21:19 |
But since the two partial instances are (conceptually, I don't care how they are implemented) two separate functions, then reasoning by analogy from two identical functions not comparing equal, I would expect two partial instances to not compare equal.
|
msg215405 - (view) |
Author: R. David Murray (r.david.murray) * |
Date: 2014-04-02 21:20 |
Of course, as soon as I hit send, I had second thoughts :). I guess a partial is a binding, not a function.
|
msg215510 - (view) |
Author: Thomas Heller (theller) * |
Date: 2014-04-04 11:33 |
My usecase is: I create kind of bound methods with functools.partial.
Apologies for the confusion by using the word 'equivalent'; what I mean is that partial instances should (IMO) compare equal when they contain the same function and args/keywords which compare equal.
I guess functools.partialmethod has the same problem and I would suggest the same fix/enhancement but I have not used them yet because I have to write python2/3 compatible code.
Anyway; if this behaviour is not seen as a bug then it is probably python-ideas material, but I'm too tired to start a discussion there atm.
|
msg215650 - (view) |
Author: Raymond Hettinger (rhettinger) * |
Date: 2014-04-06 06:48 |
> I guess a partial is a binding, not a function.
Viewed in that light, it makes sense to implement same logic as used for bound methods in method_richcompare.
|
msg260184 - (view) |
Author: shakur shams Mullick (shakur shams Mullick) * |
Date: 2016-02-12 14:21 |
I have submitted a patch for the issue. Please review and give feedback.
|
msg267300 - (view) |
Author: Jelle Zijlstra (JelleZijlstra) * |
Date: 2016-06-04 19:29 |
I think the patch needs more tests, including:
- Comparing a partial against some other objects
- Comparing two partials with a different function
- Comparing two partials with different args
You also need to check each call to PyObject_RichCompareBool for error (it returns -1 on error). I believe the current implementation would silently ignore the error. This behavior should also be tested.
Rietveld is not picking up this diff, maybe because it is a git rather than an hg diff. Generating the diff with hg will make it easier to review.
|
msg267307 - (view) |
Author: Anilyka Barry (abarry) * |
Date: 2016-06-04 19:53 |
Git diffs work, but this one doesn't apply cleanly. I manually fixed it and re-generated the patch. Rietveld should pick it up now.
Otherwise, what Jelle said. I'm not sure if the macro has the best name though - I'd probably name it `PyPartial_CheckExact` and reserve `PyPartial_Check` to allow checking for subclasses. Not sure if we need to add both in here, but it would be weird to have only one (even if the other is unused).
(Also, depending which of this one or #27137 gets committed first, the other patch will need to be updated. I'll probably do it since the other is mine.)
|
msg267321 - (view) |
Author: Anilyka Barry (abarry) * |
Date: 2016-06-04 21:25 |
New patch with Jelle's suggestions. I completely removed the macros since we need to (potentially) call Python code with PyObject_IsInstance, and need to check for return code.
I also reverted some of the unrelated changes.
|
msg267421 - (view) |
Author: Serhiy Storchaka (serhiy.storchaka) * |
Date: 2016-06-05 16:07 |
I support Jelle's comments and have added other comments.
|
msg267422 - (view) |
Author: Serhiy Storchaka (serhiy.storchaka) * |
Date: 2016-06-05 16:10 |
There is a question: should partial(func) be equal to func?
|
msg267437 - (view) |
Author: shakur shams Mullick (shakur shams Mullick) * |
Date: 2016-06-05 17:14 |
Emanuel Barry if you want to take it over, I will stop and will not modify my patch further. Otherwise please let me know.
|
msg267441 - (view) |
Author: Anilyka Barry (abarry) * |
Date: 2016-06-05 17:40 |
Shakur - Feel free to take over and work on it.
Serhiy - I don't think that partial(func) == func should be, in the same sense that (0, 1, 2), [0, 1, 2] and range(3) aren't equal even though they fundamentally represent the same thing.
|
msg267700 - (view) |
Author: shakur shams Mullick (shakur shams Mullick) * |
Date: 2016-06-07 16:08 |
Submitted a new patch addressing the review comments.
|
|
Date |
User |
Action |
Args |
2022-04-11 14:58:01 | admin | set | github: 65329 |
2021-03-19 05:13:32 | eryksun | set | versions:
+ Python 3.8, Python 3.9, Python 3.10, - Python 3.6 |
2016-06-07 16:08:41 | shakur shams Mullick | set | files:
+ issue_21130_2.patch
messages:
+ msg267700 |
2016-06-05 17:40:46 | abarry | set | messages:
+ msg267441 |
2016-06-05 17:14:13 | shakur shams Mullick | set | messages:
+ msg267437 |
2016-06-05 16:10:31 | serhiy.storchaka | set | messages:
+ msg267422 |
2016-06-05 16:07:17 | serhiy.storchaka | set | nosy:
+ serhiy.storchaka messages:
+ msg267421
|
2016-06-04 21:25:22 | abarry | set | files:
+ partial_richcompare_2.patch
messages:
+ msg267321 |
2016-06-04 19:53:49 | abarry | set | files:
+ partial_richcompare_1_regenerated.patch versions:
+ Python 3.6, - Python 3.5 nosy:
+ abarry
messages:
+ msg267307
stage: needs patch -> patch review |
2016-06-04 19:29:00 | JelleZijlstra | set | nosy:
+ JelleZijlstra messages:
+ msg267300
|
2016-03-16 23:47:08 | vstinner | set | nosy:
+ vstinner
|
2016-02-12 14:21:06 | shakur shams Mullick | set | nosy:
+ shakur shams Mullick messages:
+ msg260184
|
2016-02-12 14:18:59 | shakur shams Mullick | set | files:
+ issue21130.patch keywords:
+ patch |
2014-04-06 06:48:08 | rhettinger | set | keywords:
+ easy
messages:
+ msg215650 stage: test needed -> needs patch |
2014-04-04 21:39:04 | terry.reedy | set | stage: test needed type: behavior -> enhancement versions:
+ Python 3.5 |
2014-04-04 11:33:12 | theller | set | messages:
+ msg215510 |
2014-04-02 21:20:49 | r.david.murray | set | messages:
+ msg215405 |
2014-04-02 21:19:24 | r.david.murray | set | nosy:
+ r.david.murray messages:
+ msg215403
|
2014-04-02 20:51:27 | rhettinger | set | messages:
- msg215397 |
2014-04-02 20:19:09 | rhettinger | set | messages:
+ msg215397 |
2014-04-02 12:11:23 | eryksun | set | nosy:
+ eryksun messages:
+ msg215377
|
2014-04-02 10:39:27 | theller | set | messages:
+ msg215374 |
2014-04-02 09:56:54 | rhettinger | set | nosy:
+ rhettinger messages:
+ msg215369
|
2014-04-02 07:57:52 | theller | create | |