Skip to content

Reflection Attributes

Attribute access helpers.

Attributes Module

Attribute manipulation utilities.

This submodule provides utilities for working with object attributes dynamically.

Examples

from rite.reflection.attributes import ( ... attributes_has_attr, ... attributes_get_attr ... ) attributes_has_attr("hello", "upper") True attributes_get_attr("hello", "upper")

Modules

attributes_del_attr

Delete Attribute

Delete attribute from object.

Examples

from rite.reflection.attributes import attributes_del_attr class MyClass: ... value = 42 obj = MyClass() attributes_del_attr(obj, "value")

Functions

attributes_del_attr(obj: Any, name: str) -> None

Delete attribute from object.

Parameters:

Name Type Description Default
obj Any

Object to delete attribute from.

required
name str

Attribute name.

required

Returns:

Type Description
None

None

Raises:

Type Description
AttributeError

If attribute doesn't exist.

Examples:

>>> class MyClass:
...     value = 42
>>> obj = MyClass()
>>> attributes_del_attr(obj, "value")
>>> hasattr(obj, "value")
False
Notes

Wrapper around delattr().

attributes_get_attr

Get Attribute

Get attribute value from object.

Examples

from rite.reflection.attributes import attributes_get_attr attributes_get_attr("hello", "upper")

Functions

attributes_get_attr(obj: Any, name: str, default: Any = None) -> Any

Get attribute value from object.

Parameters:

Name Type Description Default
obj Any

Object to get attribute from.

required
name str

Attribute name.

required
default Any

Default value if not found.

None

Returns:

Type Description
Any

Attribute value or default.

Examples:

>>> attributes_get_attr("hello", "upper")
<built-in method upper of str object at 0x...>
>>> attributes_get_attr("hello", "missing", "default")
'default'
Notes

Wrapper around getattr(). Returns default if attribute not found.

attributes_has_attr

Has Attribute Checker

Check if object has an attribute.

Examples

from rite.reflection.attributes import attributes_has_attr attributes_has_attr("hello", "upper") True

Functions

attributes_has_attr(obj: Any, name: str) -> bool

Check if object has an attribute.

Parameters:

Name Type Description Default
obj Any

Object to check.

required
name str

Attribute name.

required

Returns:

Type Description
bool

True if attribute exists.

Examples:

>>> attributes_has_attr("hello", "upper")
True
>>> attributes_has_attr("hello", "nonexistent")
False
>>> attributes_has_attr([], "append")
True
Notes

Wrapper around hasattr().

attributes_set_attr

Set Attribute

Set attribute value on object.

Examples

from rite.reflection.attributes import attributes_set_attr class MyClass: ... pass obj = MyClass() attributes_set_attr(obj, "value", 42) obj.value 42

Functions

attributes_set_attr(obj: Any, name: str, value: Any) -> None

Set attribute value on object.

Parameters:

Name Type Description Default
obj Any

Object to set attribute on.

required
name str

Attribute name.

required
value Any

Value to set.

required

Returns:

Type Description
None

None

Examples:

>>> class MyClass:
...     pass
>>> obj = MyClass()
>>> attributes_set_attr(obj, "value", 42)
>>> obj.value
42
Notes

Wrapper around setattr(). Creates attribute if doesn't exist.

options: show_root_heading: true show_source: false heading_level: 2