Skip to main content
`Ctrl`+`K`

# Unit

class astropy.units.Unit(s='', represents=None, format=None, namespace=None, doc=None, parse_strict='raise')

  Bases: `NamedUnit`

  The main unit class.

  There are a number of different ways to construct a Unit, but always returns a `UnitBase` instance. If the arguments refer to an already-existing unit, that existing unit instance is returned, rather than a new one.

  - From a string:

        Unit(s, format=None, parse_strict='silent')

    Construct from a string representing a (possibly compound) unit.

    The optional `format` keyword argument specifies the format the string is in, by default `"generic"`. For a description of the available formats, see `astropy.units.format`.

    The optional `parse_strict` keyword argument controls what happens when the string does not comply with the specified format. It may be one of the following:

    > - `'raise'`: (default) raise a `ValueError` exception.

    > - `'warn'`: emit a `UnitParserWarning`, and return a unit.

    > - `'silent'`: return a unit silently.

    With `'warn'` or `'silent'` the parser might be able to parse the string and return a normal unit, but if it fails then an `UnrecognizedUnit` instance is returned.

  - From a number:

        Unit(number)

    Creates a dimensionless unit.

  - From a `UnitBase` instance:

        Unit(unit)

    Returns the given unit unchanged.

  - From no arguments:

        Unit()

    Returns the dimensionless unit.

  - The last form, which creates a new `Unit` is described in detail below.

  See also: https://docs.astropy.org/en/stable/units/

  Parameters:
    st `python:str` or `python:list` of `python:str`

      The name of the unit. If a list, the first element is the canonical (short) name, and the rest of the elements are aliases.

    represents astropy:unit-like, optional

      The unit that this named unit represents.

    doc `python:str`, optional

      A docstring describing the unit.

    format `python:dict`, optional

      A mapping to format-specific representations of this unit. For example, for the `Ohm` unit, it might be nice to have it displayed as `\Omega` by the `latex` formatter. In that case, `format` argument should be set to:

          {'latex': r'\Omega'}

    namespace `python:dict`, optional

      When provided, inject the unit (and all of its aliases) into the given namespace.

  Raises:
    `ValueError`

      If any of the given unit names are already in the registry.

    `ValueError`

      If any of the given unit names are not valid Python tokens.

    `ValueError`

      If `represents` cannot be parsed as a unit, e.g., because it is a malformed string or a |Quantity| that is not a scalar.

  Attributes Summary

  `represents` | The unit that this named unit represents.

  Methods Summary

  `decompose`([bases]) | Return a unit object composed of only irreducible units.
  `is_unity`() | Check whether the unit is unscaled and dimensionless.

  Attributes Documentation

  represents

    The unit that this named unit represents.

  Methods Documentation

  decompose(bases=())

    Return a unit object composed of only irreducible units.

    Parameters:
      bases python:sequence of `UnitBase`, optional

        The bases to decompose into. When not provided, decomposes down to any irreducible units. When provided, the decomposed result will only contain the given units. This will raises a `UnitsError` if it’s not possible to do so.

    Returns:
      unit `CompositeUnit`

        New object containing only irreducible unit objects.

  is_unity()

    Check whether the unit is unscaled and dimensionless.