def_unit#
- astropy.units.def_unit(s: str | list[str], represents: UnitBase, doc: str | None = None, format: Mapping[str, str] | None = None, prefixes: bool | Iterable[UnitPrefix] = False, exclude_prefixes: Collection[str] = (), namespace: MutableMapping[str, object] | None = None) Unit [source]#
- astropy.units.def_unit(s: str | list[str], represents: None = None, doc: str | None = None, format: Mapping[str, str] | None = None, prefixes: bool | Iterable[UnitPrefix] = False, exclude_prefixes: Collection[str] = (), namespace: MutableMapping[str, object] | None = None) IrreducibleUnit
Define a new unit.
This function differs from creating units directly with
Unit
orIrreducibleUnit
because it can also automatically generate prefixed units in the given namespace.- Parameters:
- s
str
orlist
ofstr
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
UnitBase
, optional The unit that this named unit represents. If not provided, a new
IrreducibleUnit
is created.- doc
str
, optional A docstring describing the unit.
- format
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 thelatex
formatter. In that case,format
argument should be set to:{'latex': r'\Omega'}
- prefixesbool or iterable of
UnitPrefix
, optional When
True
, generate all of the SI prefixed versions of the unit as well. For example, for a given unitm
, will generatemm
,cm
,km
, etc. If only a few prefixed versions should be created then an iterable ofUnitPrefix
instances can be specified instead. Default isFalse
, which means no prefixed versions will be generated.This function always returns the base unit object, even if multiple scaled versions of the unit were created.
- exclude_prefixes
Collection
ofstr
, optional If any of the SI prefixes need to be excluded, they may be listed here. For example, when defining the prefixes for
a
,exclude_prefixes
should be set to["P"]
so thatPa
would still refer to the pascal.If a bare
str
is used then the prefixes that will be excluded are the substrings of thestr
, not just its individual characters.- namespace
dict
, optional When provided, inject the unit (and all of its aliases and prefixes), into the given namespace dictionary.
- s
- Returns:
- unit
NamedUnit
The newly-defined unit, or a matching unit that was already defined.
- unit