I am working on a graph library in Python and I am defining my vetex this way:
class Vertex:
def __init__(self,key,value):
self._key = key
self._value = value
@property
def key(self):
return self._key
@key.setter
def key(self,newKey):
self._key = newKey
@property
def value(self):
return self._value
@value.setter
def value(self,newValue):
self.value = newValue
def _testConsistency(self,other):
if type(self) != type(other):
raise Exception("Need two vertexes here!")
def __lt__(self,other):
_testConsistency(other)
if self.index <= other.index:
return True
return False
......
Do I really have to define __lt__,__eq__,__ne__….all by my self? It is so verbose. Is there simpler way I can get around this?
Cheers.
Please dont use __cmp__ since it will be away in python 3.
functools.total_ordering can help you out here. It’s meant to be a class decorator. You define one of
__lt__(),__le__(),__gt__(), or__ge__()AND__eq__and it fills in the rest.As a side note:
Instead of writing this
write this:
It’s cleaner that way. 🙂