Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numbers
- import operator
- import random
- class RandomInt(object):
- def __init__(self, a, b):
- self.a, self.b = a, b
- def __int__(self):
- return random.randint(self.a, self.b)
- def __float__(self):
- return float(int(self))
- def __repr__(self):
- return repr(int(self))
- def __hash__(self):
- raise NotImplemented
- for op in ('add', 'sub', 'mul', 'truediv', 'floordiv',
- 'mod', 'divmod', 'pow', 'lshift', 'rshift',
- 'and_', 'xor_', 'or_'):
- def foo(self, rhs, op=op):
- return getattr(operator, op)(int(self), rhs)
- def rfoo(self, lhs, op=op):
- return getattr(operator, op)(lhs, int(self))
- setattr(RandomInt, '__' + op.rstrip('_') + '__', foo)
- setattr(RandomInt, '__r' + op.rstrip('_') + '__', rfoo)
- for op in 'neg', 'pos', 'abs', 'invert':
- def foo(self, op=op):
- return getattr(operator, op)(int(self))
- setattr(RandomInt, '__' + op.rstrip('_') + '__', foo)
- numbers.Integral.register(RandomInt)
- a = RandomInt(4, 11)
- print(a)
- print(a + a)
- print(a + 3)
- print(3 + a)
- print(float(a))
- print(complex(a))
- print(round(a))
- print(repr(a))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement