attribute_call_string
- plasmapy.utils.code_repr.attribute_call_string(
- cls,
- attr: str,
- args_to_cls: tuple | Any | None = None,
- kwargs_to_cls: dict[str, Any] | None = None,
- max_items: int = 12,
Approximate the command to instantiate a class, and access an attribute of the resulting class instance.
- Parameters:
cls (
class
) – The class to be used in the string representation.attr (
str
) – The name of the attribute of classcls
.args_to_cls (
tuple
,list
, or anyobject
; optional) – Atuple
orlist
containing positional arguments, or any other type ofobject
if there is only one positional argument, to be used during instantiation ofcls
kwargs_to_cls (
dict
, optional) – Adict
containing the keyword arguments to be used during instantiation ofcls
.max_items (
int
, default: 12) – The maximum number of items to include in andarray
orQuantity
; additional items will be truncated with an ellipsis.
- Returns:
Approximation of a command to instantiate
cls
withargs_to_cls
as positional arguments andkwargs_to_cls
as keyword arguments, and then access the attributeattr
.- Return type:
See also
Notes
This function will generally provide an exact call string for most common types of simple positional and keyword arguments. When dealing with types that are not accounted for, this function will fall back on
repr
.This function assumes aliases of
u
forastropy.units
andnp
fornumpy
.Examples
>>> class SampleClass: ... def __init__(self, arg1, kwarg1=None): ... pass ... ... @property ... def attribute(self): ... return 42 >>> args_to_cls = (1, 2) >>> kwargs_to_cls = {"kwarg1": 2} >>> attribute_call_string(SampleClass, "attribute", args_to_cls, kwargs_to_cls) 'SampleClass(1, 2, kwarg1=2).attribute'