Unit#
- class astropy.units.Unit(s='', represents=None, format=None, namespace=None, doc=None, parse_strict='raise')[source]#
Bases:
NamedUnitThe main unit class.
There are a number of different ways to construct a Unit, but always returns a
UnitBaseinstance. 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
formatkeyword argument specifies the format the string is in, by default"generic". For a description of the available formats, seeastropy.units.format.The optional
parse_strictkeyword argument controls what happens when the string does not comply with the specified format. It may be one of the following:'raise': (default) raise aValueErrorexception.'warn': emit aUnitParserWarning, 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 anUnrecognizedUnitinstance is returned.From a number:
Unit(number)
Creates a dimensionless unit.
From a
UnitBaseinstance:Unit(unit)
Returns the given unit unchanged.
From no arguments:
Unit()
Returns the dimensionless unit.
The last form, which creates a new
Unitis described in detail below.
See also: https://docs.astropy.org/en/stable/units/
- Parameters:
- st
python:strorpython:listofpython: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
UnitBaseinstance 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
Ohmunit, it might be nice to have it displayed as\Omegaby thelatexformatter. In that case,formatargument 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.
- st
- Raises:
ValueErrorIf any of the given unit names are already in the registry.
ValueErrorIf any of the given unit names are not valid Python tokens.
Attributes Summary
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={})[source]#
Return a unit object composed of only irreducible units.
- Parameters:
- basespython: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
UnitsErrorif it’s not possible to do so.
- basespython:sequence of
- Returns:
- unit
CompositeUnit New object containing only irreducible unit objects.
- unit