lundi 2 mars 2015

Strategy for "multi-level equality" test


It is well-known that Lisp supports multi-level equality test.


I want to implement similar multi-level equality test on a custom class in Python.


I know that I can (and should) override the __eq__, __neq__, and __hash__ methods, but these three together provides only a single-level equality test.


My custom class can have several levels of 'similarity', e.g.:



  • Similar in hash (CRC64-ECMA) only

  • Similar in hash and flags

  • Similar in hash, flags, and price (in-game price; this is a data structure for an in-game object)

  • Similar in hash, name, price, and name


My questions:



  1. If I override the three equality-testing methods, should they test against the most generic equality (hash only) or against the most specific equality (all the properties)?

  2. Would it be better to provide several different methods, one for each 'level' of equality, or one method with an equality_level= kwarg?

  3. Or, alternatively, provide one method without any kwarg, but returning a value indicating the 'level' of equality (thus, not returning a bool)?


Thank you for your help.





Aucun commentaire:

Enregistrer un commentaire