0

I was trying the following code,

from pint import UnitRegistry

ureg = UnitRegistry()
Q_ = ureg.Quantity

class Simple:
    @ureg.wraps(None, (None, None, 'm**3'), True)
    def __init__(self, a, b):
        self.a = a
        self.b = b

    def calculate(self):
        return self.a*self.b

if __name__ == "__main__":
    c = Simple(2, Q_(20, 'm**3')).calculate()
    print c

which gives the output,

40.0

This is a float type. Is there a way to instead automatically output a pint Quantity object with magnitude and correct units, i.e. 40.0 m**3?

Thanks

aaron02
  • 320
  • 2
  • 14
  • 1
    You would presumably also have to wrap `calculate`, I doubt `pint` will follow those attributes through the class. – jonrsharpe Sep 13 '16 at 19:25
  • That works. I was hoping that the class variables 'self.a' and 'self.b' would be Quantity objects (which they are not), and hence the result of their multiplication would also be a Quantity object that would be returned. – aaron02 Sep 13 '16 at 23:53

0 Answers0