Subclassing int for a revolving control number

Michele Simionato michele.simionato at gmail.com
Sun Jul 18 16:32:35 CEST 2004


Chris Cioffi <evenprimes at gmail.com> wrote in message news:<mailman.508.1090008510.5135.python-list at python.org>...
> Hello all!
> 
> I'm trying to subclass int such that once it reaches a certain value,
> it flips back to the starting count.  This is for use as an X12
> control number.  Subclassing int and getting the display like I want
> it was trivial, however what's the best way to make it such that:
> 990 + 1 = 991 
> 991 + 1 = 992
> ...
> 998 + 1 = 999
> 999 + 1 = 001
> 
> Should I set any of the special functions like __sub__ to raise
> NotImpemented and just make my own custom assignment and __add__
> functions?  (The other fucntions would make no sense in this
> context...)
> 
> Is there an "on change" kind of meta-function?  (Like the onChange
> event in a GUI text control)

If you are not interested in subtracting the counter, this is a nice
solution:

>>> from itertools import cycle
>>> for i in cycle(range(1,1000)):
...     print i

CTRL-C when you are tired ;)


        Michele Simionato



More information about the Python-list mailing list