division
Jeff Epler
jepler at unpythonic.net
Mon Jun 23 15:24:37 EDT 2003
>>> class crankyint(int):
... def __div__(self, other):
... if self % other:
... raise ValueError, "cranky"
... return crankyint(int.__div__(self, other))
...
>>> a, b, c = map(crankyint, (2, 3, 4))
>>> print c/a
2
>>> print b/a
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 4, in __div__
ValueError: cranky
You'll have to override other arithmetic functions to return crankyints.
Something like:
for method_name in "__add__ __subtract__ __neg__ __mul__".split():
exec "def %s(*args): return crankyint(int.%s(*args))"
then, force all the inputs to be crankyints instead of ints.
Jeff
More information about the Python-list
mailing list