Subclassing int for a revolving control number
Scott David Daniels
Scott.Daniels at Acm.Org
Sat Jul 17 15:32:04 EDT 2004
Chris Cioffi wrote:
> I'm trying to subclass int such that once it reaches a certain value,
> it flips back to the starting count....
Others have given you the real answer -- don't bother with this.
However, it looks like fun:
class Clock(int):
def __add__(self, other):
return self.__class__(super(Clock, self
).__add__(other) % 12 or 12)
__radd__ = __add__
def __sub__(self, other):
return self + -other
def __rsub__(self, other):
return self.__class__(-self + 12) + other
def __repr__(self):
return '%s(%s)' % (type(self).__name__, int(self))
-Scott David Daniels
Scott.Daniels at Acm.Org
More information about the Python-list
mailing list