python - reflected greater than magic methods -


i in need of reflected magic method "greater than" , there not appear one. here situation. have class keeps track of units. call property. have magic method setup handle comparisons, not work when put property on right side. here example:

class property(): def __init__(self, input, units):     self.value = input     self.units = units   def __gt__(self, other):     if isinstance(other, property):         return self.value.__gt__(other.value)     else:         return self.value.__gt__(other)  def __float__(self):     return float(self.value)  if __name__=='__main__':      x = property(1.,'kg')     y = property(0.,'kg')     print y > x     print float(y) > x     print y > float(x) 

so if run see output is: false, true, false because middle example executing float > property uses built in > not > have defined using magic methods. need magic method used when property on right hand side. not thing? if not, how can write combination of values , own class can compared. not have rules comparisons. ie, don't want never able compare float property.

you can use functools.total_ordering decorator create missing comparison methods you:

import functools  @functools.total_ordering class property():     ... 

then false, false, false. make sure read documentation, though.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -