[Python] Making a python user-defined class sortable, hashable

Discussão em 'Python' iniciado por Stack, Abril 30, 2025.

  1. Stack

    Stack Membro Participativo

    What methods need to be overridden/implemented when making user-defined classes sortable and/or hashable in python?

    What are the gotchas to watch out for?

    I type dir({}) into my interpreter to get a list of methods on built-in dicts. Of those, I assume I need to some implement some subset of

    ['__cmp__', '__eq__', '__ge__', '__gt__', '__hash__', '__le__', '__lt__', '__ne__']


    Is there a difference in which methods must be implemented for Python3 as opposed to Python2?

    Continue reading...

Compartilhe esta Página