Comparable type in expansion module

To implement! = And == for my CPython extension type, should I implement tp_compare, tp_richcompareor both?

Under what circumstances is each of them called?

+3
source share
1 answer

tp_richcomparesimilar to the rich comparison of special methods in the Python language itself. It is selected in preference tp_comparewhen comparison operators are called in a class.

tp_richcompare, . , , . , memcmp(a, b, sizeof(*a)) == 0 , , memcmp(a, b, sizeof(*a)) < 0 , a b, . tp_compare , -1, 0, 1. tp_richcompare, OTOH, , , , , .

tp_richcompare , , , u < v, u v .

+6

Source: https://habr.com/ru/post/1746189/


All Articles