Hướng dẫn dùng comparable python python
Ngày đăng:
02/09/2022
Trả lời:
0
Lượt xem:
114
For a full set of comparison functions I have used the following mixin, which you could put in say for example a mixin.py in your module.
To use the mixin above you need to implement a _cmpkey() method that returns a key of objects that can be compared, similar to the key() function used when sorting. The implementation could look like this:
The reason I use this instead of the total_ordering recipe is this bug. It's fixed in Python 3.4, but often you need to support older Python versions as well. |