Keeping A Variable Inside A Range In Python
i am trying to write a code for the game pong but i am facing a problem when trying to control the range of the paddles positions , the question is that : is there a way in python
Solution 1:
You could define your own "bounded" numeric type. For example if paddle1_pos[1]
was an integer value you could create a class like the following and use it instead
classBoundedInt(int):
def__new__(cls, *args, **kwargs):
lower, upper = bounds = kwargs.pop('bounds')
val = int.__new__(cls, *args, **kwargs) # supports all int() args
val = lower if val < lower else upper if val > upper else val
val = super(BoundedInt, cls).__new__(cls, val)
val._bounds = bounds
return val
def__add__(self, other):
return BoundedInt(int(self)+other, bounds=self._bounds)
__iadd__ = __add__
def__sub__(self, other):
return BoundedInt(int(self)-other, bounds=self._bounds)
__isub__ = __sub__
def__mul__(self, other):
return BoundedInt(int(self)*other, bounds=self._bounds)
__imul__ = __mul__
# etc, etc...if __name__ == '__main__':
v = BoundedInt(100, bounds=(0, 100))
printtype(v), v
v += 10printtype(v), v
w = v + 10printtype(w), w
x = v - 110printtype(x), x
Output:
<class '__main__.BoundedInt'> 100
<class '__main__.BoundedInt'> 100
<class '__main__.BoundedInt'> 100
<class '__main__.BoundedInt'> 0
Solution 2:
For completeness here's another answer which shows how to programmatically add all the arithmetic methods that integers have to the custom class using a metaclass. Note it's unclear whether it makes sense in every case to return a BoundedInt with the same bounds as the operand. The code is also compatible with both Python 2 & 3.
classMetaBoundedInt(type):
# int arithmetic methods that return an int
_specials = ('abs add and div floordiv invert lshift mod mul neg or pos ''pow radd rand rdiv rfloordiv rlshift rmod rmul ror rpow ''rrshift rshift rsub rtruediv rxor sub truediv xor').split()
_ops = set('__%s__' % name for name in _specials)
def__new__(cls, name, bases, attrs):
classobj = type.__new__(cls, name, bases, attrs)
# create wrappers for specified arithmetic operationsfor name, meth in ((n, m) for n, m invars(int).items() if n in cls._ops):
setattr(classobj, name, cls._WrappedMethod(cls, meth))
return classobj
class_WrappedMethod(object):
def__init__(self, cls, func):
self.cls, self.func = cls, func
def__get__(self, obj, cls=None):
defwrapper(*args, **kwargs):
# convert result of calling self.func() to cls instancereturn cls(self.func(obj, *args, **kwargs), bounds=obj._bounds)
for attr in'__module__', '__name__', '__doc__':
setattr(wrapper, attr, getattr(self.func, attr, None))
return wrapper
defwith_metaclass(meta, *bases):
""" Py 2 & 3 compatible way to specifiy a metaclass. """return meta("NewBase", bases, {})
classBoundedInt(with_metaclass(MetaBoundedInt, int)):
def__new__(cls, *args, **kwargs):
lower, upper = bounds = kwargs.pop('bounds')
val = int.__new__(cls, *args, **kwargs) # supports all int() args
val = super(BoundedInt, cls).__new__(cls, min(max(lower, val), upper))
val._bounds = bounds
return val
if __name__ == '__main__':
# all results should be BoundInt instances with values within bounds
v = BoundedInt('64', 16, bounds=(0, 100)) # 0x64 == 100print('type(v)={}, value={}, bounds={}'.format(type(v).__name__, v, v._bounds))
v += 10print('type(v)={}, value={}, bounds={}'.format(type(v).__name__, v, v._bounds))
w = v + 10print('type(w)={}, value={}, bounds={}'.format(type(w).__name__, w, w._bounds))
x = v - 110print('type(x)={}, value={}, bounds={}'.format(type(x).__name__, x, x._bounds))
Output:
type(v)=BoundedInt, value=100, bounds=(0, 100)
type(v)=BoundedInt, value=100, bounds=(0, 100)
type(w)=BoundedInt, value=100, bounds=(0, 100)
type(x)=BoundedInt, value=0, bounds=(0, 100)
Post a Comment for "Keeping A Variable Inside A Range In Python"