I have a class called Cell:
class Cell:
def __init__(self, value, color, size):
self._value = value
self._color = color
self._size = size
# and other methods...
Cell._value
will store a string, integer, etc. (whatever I am using that object for). I want all default methods that would normally use the "value" of an object to use <Cell object>._value
so that I can do:
>>> c1 = Cell(7, "blue", (5,10))
>>> c2 = Cell(8, "red", (10, 12))
>>> print c1 + c2
15
>>> c3 = Cell(["ab", "cd"], "yellow", (50, 50))
>>> print len(c3), c3
2 ['ab', 'cd']
# etc.
I could override all the default methods:
class Cell:
def __init__(self, value, color, size):
# ...
def __repr__(self):
return repr(self._value)
def __str__(self):
return str(self._value)
def __getitem__(self, key):
return self._value[key]
def __len__(self):
return len(self._value)
# etc.
...but is there an easier way?