public
Description: Add rich comparison special methods (__ne__, __gt__, etc.) to a class
Homepage:
Clone URL: git://github.com/ndanger/pycomparable.git
pycomparable / README
100644 42 lines (31 sloc) 1.912 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 
DISCLAIMER:
 
Although this works (it really adds the rich comparison operators), it's for
fun/learning purposes only. If you really need all the comparisons, you're
probably better off writing __cmp__. I think Python3000 might automatically
create __ne__ based on __eq__. The one real world use might be to easily
add all rich comparison methods to a subclass where the parent class
implements a subset of the operator. But how often does that happen? Just
use it for fun.
 
 
Try to add rich comparison methods (__eq__, __ne__, etc.) to an existing class.
It exports three names:
 
* comparable - class decorator
* ComparableMetaclass - Metaclass to automatically call comparable
* ComparableMixin - Mixin to automatically call comparable
 
comparable works as a class decorator. It takes a class as an argument and
returns a decorated class. comparable attempts to add the rich comparison
special methods if they are not defined. If __eq__ is not defined, it will
try to define it as being not != or not (< or >), etc. It then sets != as
the inversion of ==. Finally it sets whatever inequalities are possible
(e.g. < === <= and not ==).
 
comparable will not overwrite existing methods.
 
comparable won't add methods if there is not enough information. For
example, if only __eq__ and __ne__ are defined, it will not attempt to add
any inequalities. It also won't raise any errors, so be warned.
 
ComparableMetaclass and ComparableMixin are additional ways to create a
comparable class. Both are simply wrappers for the main comparable
function.
 
Inspired by this discussion turned rant on comp.lang.python
(http://groups.google.com/group/comp.lang.python/browse_thread/thread/a5fa8ff0ffadd6ee/1aa3b5d25eae91d5)
Loosely based on Ruby's comparable module
(http://www.ruby-doc.org/core/classes/Comparable.html) though I believe the
implementation is different.