As an alternative to mgilson's excellent response, you can subclass int into a custom class to achieve this
>>> class V(int): ... def __new__(cls,val,**kwargs): ... return super(V,cls).__new__(cls,max(val,0))
Then use it directly:
>>> A=V(200) 200 >>> B=V(-140) 0 >>> [V(i) for i in [200, -140, 400, -260]] [200, 0, 400, 0] >>> A,B,C,D = [V(i) for i in [200, -140, 400, -260]]
The only advantage that can be done this way is that you can override __sub__ and __add__ __mul__ , and then you will always be greater than V if you have a=V(50)-100
Example:
>>> class V(int): ... def __new__(cls,val,**kwargs): ... return super(V,cls).__new__(cls,max(val,0)) ... def __sub__(self,other): ... if int.__sub__(self,other)<0: ... return 0 ... else: ... return int.__sub__(self,other) >>> a=V(50) >>> b=V(100) >>> ab
source share