2

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?

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
  • 2
    The tutorial touches on this. You can have multiple registries, each with their own units defined. It explicitly warns against something like `10 * UnitRegistry().meter + 10 * UnitRegistry().meter`; only units from the same registry should be considered compatible. Unfortunately, the tutorial (and the documentation as a whole) seems to lack a good example of why you might want or need a registry other than the default. – chepner Jun 12 '18 at 15:17

1 Answers1

2

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

Jonathan
  • 748
  • 3
  • 20