Parameter

class astropy.cosmology.Parameter(fget=None, doc=None, *, unit=None, equivalencies=[], fmt='.3g')[source]

Bases: object

Cosmological parameter (descriptor).

Should only be used with a Cosmology subclass. For automatic default value and unit inference make sure the Parameter attribute has a corresponding initialization argument (see Examples below).

Parameters
fgetcallable() or None, optional

Function to get the value from instances of the cosmology class. If None (default) returns the corresponding private attribute. Often not set here, but as a decorator with getter.

docstr or None, optional

Parameter description. If ‘doc’ is None and ‘fget’ is not, then ‘doc’ is taken from fget.__doc__.

unitastropy:unit-like or None (optional, keyword-only)

The Unit for the Parameter. If None (default) no unit as assumed.

equivalenciesEquivalency or sequence thereof

Unit equivalencies for this Parameter.

fmtstr (optional, keyword-only)

format specification, used when making string representation of the containing Cosmology. See https://docs.python.org/3/library/string.html#formatspec

Examples

The most common use case of Parameter is to access the corresponding private attribute.

>>> from astropy.cosmology import LambdaCDM
>>> from astropy.cosmology.core import Parameter
>>> class Example1(LambdaCDM):
...     param = Parameter(doc="example parameter", unit=u.m)
...     def __init__(self, param=15 * u.m):
...         super().__init__(70, 0.3, 0.7)
...         self._param = param << self.__class__.param.unit
>>> Example1.param
<Parameter 'param' at ...
>>> Example1.param.unit
Unit("m")
>>> ex = Example1(param=12357)
>>> ex.param
<Quantity 12357. m>

Parameter also supports custom getter methods. m_nu is a good example.

>>> import astropy.units as u
>>> class Example2(LambdaCDM):
...     param = Parameter(doc="example parameter", unit="m")
...     def __init__(self, param=15):
...         super().__init__(70, 0.3, 0.7)
...         self._param = param << self.__class__.param.unit
...     @param.getter
...     def param(self):
...         return self._param << u.km
>>> ex2 = Example2(param=12357)
>>> ex2.param
<Quantity 12.357 km>
:hide:

>>> from astropy.cosmology.core import _COSMOLOGY_CLASSES
>>> _ = _COSMOLOGY_CLASSES.pop(Example1.__qualname__)
>>> _ = _COSMOLOGY_CLASSES.pop(Example2.__qualname__)

Attributes Summary

equivalencies

Equivalencies used when initializing Parameter.

format_spec

String format specification.

name

Parameter name.

unit

Parameter unit.

Methods Summary

getter(fget)

Make new Parameter with custom fget.

Attributes Documentation

equivalencies

Equivalencies used when initializing Parameter.

format_spec

String format specification.

name

Parameter name.

unit

Parameter unit.

Methods Documentation

getter(fget)[source]

Make new Parameter with custom fget.

Note: Parameter.getter must be the top-most descriptor decorator.

Parameters
fgetcallable()
Returns
Parameter

Copy of this Parameter but with custom fget.