Unit comparisons? #1692
-
I'm having trouble understanding what is compared between two quantities -- seems like only magnitudes? import pint
ureg1 = pint.UnitRegistry()
ureg2 = pint.UnitRegistry()
ureg1 == ureg2
# >> False
q1 = ureg1.Quantity(0,"W")
q2 = ureg2.Quantity(0,"W")
q1 == q2
# >> True
# Still `True` if q1 and q2 have a non-zero value:
q1 = ureg1.Quantity(1,"W")
q2 = ureg2.Quantity(1,"W")
q1 == q2
# >> True
ureg1.Quantity == ureg2.Quantity
# >> False
q2.units == q1.units
# >> ValueError: Cannot operate with Unit and Unit of different registries.
q1.magnitude == q2.magnitude
# >> True Is that meant to be the case? I would have expected that comparing two quantities from different registries would throw an error because their units don't match. What am I missing here? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Comparing units from different registries is not well-defined, and therefore it is an invalid operation. You should not expect a valid result. We rely on Python's consenting adults philosophy. If you are interested in the details, to avoid a performance hit |
Beta Was this translation helpful? Give feedback.
Comparing units from different registries is not well-defined, and therefore it is an invalid operation. You should not expect a valid result. We rely on Python's consenting adults philosophy.
If you are interested in the details, to avoid a performance hit
Quantity._eq__
comparesq2._units == q1._units
, notq2.units == q1.units