0

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.

Bob Fang
  • 6,963
  • 10
  • 39
  • 72
  • 5
    Why the properties? Why not just use regular attributes? –  Jan 10 '13 at 19:21
  • Umm @delnan http://stackoverflow.com/questions/6618002/python-property-versus-getters-and-setters this thread may help you – Bob Fang Jan 10 '13 at 19:23
  • 1
    I know perfectly well how `property` works and why it's good. But it's still at least six lines more than just using an attribute (`self.key = ...` in `__init__`). –  Jan 10 '13 at 19:25
  • 3
    I think that delnan's point is that you can define `key` and `value` as regular attributes (no need for the `_`) and then you don't need the properties. If you ever decide you *need* a property, then you can adjust the code and it will still be backward compatable. – mgilson Jan 10 '13 at 19:26
  • +1 to delnan and mgilson's points, it's very bad form to define getters and setters, even with the property decorator, until you actually need them. – Daniel Roseman Jan 10 '13 at 19:32
  • Oh good point, I used to program in java and I am more comfortable with setters and getters. Thanks, I will see which way is better by using them both and experiment. Thanks a lot for point out. – Bob Fang Jan 10 '13 at 19:33
  • My question is, under what situation, a setter and getter is necessary for you in python? – Bob Fang Jan 10 '13 at 19:41
  • @dorafmon Never, unless you're stuck with maintaining compatibility with a horrible API that does it. Properties are what you generally use when you'd use getters/setters in other languages. But if they do nothing (except, of course, delegating to an attribute), you don't need them. Only use them when you have additional logic. –  Jan 10 '13 at 19:47

2 Answers2

5

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

if self.index <= other.index:
    return True
return False

write this:

return self.index <= other.index

It's cleaner that way. :-)

mgilson
  • 300,191
  • 65
  • 633
  • 696
  • Do I have to import anything to use this? I got error using @total_ordering. Thanks for your answer. – Bob Fang Jan 10 '13 at 19:40
2

Using functools.total_ordering, you only need to define one of the equality operators and one of the ordering operators. In Python < 3.2, you're out of luck, something has to define these operators as individual methods. Though you may be able to save some code by writing a simpler version of total_ordering yourself, if you need it in several places.