Search code examples
pythonpint

Why does pint need a unit registry?


The default example for pint is

>>> import pint
>>> ureg = pint.UnitRegistry()
>>> 3 * ureg.meter + 4 * ureg.cm
<Quantity(3.04, 'meter')>

which makes me wonder what I need the unit registry object for. For example, I could imagine it to be just a submodule:

>>> from pint import ureg
>>> 3 * ureg.meter + 4 * ureg.cm
<Quantity(3.04, 'meter')>

What is the advantage of ureg being an object instead of a submodule?

Do I have to share this ureg object or can I simply create a new one when I make multiple calls in different functions? Or is it possibly a singleton, so it doesn't matter as there will always just be one?


Solution

  • Your unit registry defines and handles units for you. You could define other units/conversions in your registry so there is no universal registry, there is just one base registry which you can extend.

    On how to extent see the section on Defining units