Skip to content
New: Star-import, signal unraveling, Python 3.12 support, and more

# attr_ module

Utilities for working with class/instance attributes.

## deep_getattr function

    deep_getattr(
        obj,
        attr_chain,
        getattr_func=<function default_getattr_func>,
        call_last_attr=True
    )

Retrieve attribute consecutively.

The attribute chain `attr_chain` can be:

- string -> get variable/property or method without arguments
- tuple of string -> call method without arguments
- tuple of string and tuple -> call method and pass positional arguments (unpacked)
- tuple of string, tuple, and dict -> call method and pass positional and keyword arguments (unpacked)
- iterable of any of the above

Use `getattr_func` to overwrite the default behavior of accessing an attribute (see default_getattr_func()).

Hint

If your chain includes only attributes and functions without arguments, you can represent this chain as a single (but probably long) string.

## default_getattr_func function

    default_getattr_func(
        obj,
        attr,
        args=None,
        kwargs=None,
        call_attr=True
    )

Default `getattr_func`.

## get_dict_attr function

    get_dict_attr(
        obj,
        attr
    )

Get attribute without invoking the attribute lookup machinery.

## parse_attrs function

    parse_attrs(
        obj,
        own_only=False,
        sort_by=None
    )

Parse attributes of a class, object, or a module, and return a DataFrame with types and paths.

## AttrResolverMixin class

    AttrResolverMixin()

Class that implements resolution of self and its attributes.

Resolution is `getattr` that works for self, properties, and methods. It also utilizes built-in caching.

Subclasses

- Wrapping

### cls_dir method

Get set of attribute names.

### deep_getattr method

    AttrResolverMixin.deep_getattr(
        *args,
        **kwargs
    )

See deep_getattr().

### post_resolve_attr method

    AttrResolverMixin.post_resolve_attr(
        attr,
        out,
        final_kwargs=None
    )

Post-process an object after resolution.

Must return an object.

### pre_resolve_attr method

    AttrResolverMixin.pre_resolve_attr(
        attr,
        final_kwargs=None
    )

Pre-process an attribute before resolution.

Must return an attribute.

### resolve_attr method

    AttrResolverMixin.resolve_attr(
        attr,
        args=None,
        cond_kwargs=None,
        kwargs=None,
        custom_arg_names=None,
        cache_dct=None,
        use_caching=True,
        passed_kwargs_out=None,
        use_shortcuts=True
    )

Resolve an attribute using keyword arguments and built-in caching.

- If there is a `get_{arg}` method, uses `get_{arg}` as `attr`.
- If `attr` is a property, returns its value.
- If `attr` is a method, passes `*args`, `**kwargs`, and `**cond_kwargs` with keys found in the signature.
- If `use_shortcuts` is True, resolves the potential shortcuts using AttrResolverMixin.resolve_shortcut_attr().

Won't cache if `use_caching` is False or any passed argument is in `custom_arg_names`.

Use `passed_kwargs_out` to get keyword arguments that were passed.

### resolve_self method

    AttrResolverMixin.resolve_self(
        cond_kwargs=None,
        custom_arg_names=None,
        impacts_caching=True,
        silence_warnings=False
    )

Resolve self.

Note

`cond_kwargs` can be modified in-place.

### resolve_shortcut_attr method

    AttrResolverMixin.resolve_shortcut_attr(
        attr,
        *args,
        **kwargs
    )

Resolve an attribute that may have shortcut properties.

### self_aliases property

Names to associate with this object.

#### Cookie consent

We use cookies to recognize your repeated visits and preferences, as well as to measure the effectiveness of our documentation and whether users find what they're searching for. With your consent, you're helping us to make our documentation better.

- Google Analytics
- GitHub

Manage settings