espnet2.asr.state_spaces.utils.extract_attrs_from_obj
Less than 1 minute
espnet2.asr.state_spaces.utils.extract_attrs_from_obj
espnet2.asr.state_spaces.utils.extract_attrs_from_obj(obj, *attrs)
Extract specified attributes from an object.
This function retrieves the values of specified attributes from the given object. If the object is None, an empty list is returned. If an attribute does not exist on the object, None is returned for that attribute.
- Parameters:
- obj – The object from which to extract attributes. If None, return an empty list.
- *attrs – A variable number of attribute names to extract from the object.
- Returns: A list of values corresponding to the specified attributes. The list will contain None for any attributes that do not exist on the object.
Examples
class Sample: : def __init__(self): : self.attr1 = “value1” self.attr2 = “value2”
sample_obj = Sample()
Extract existing attributes
values = extract_attrs_from_obj(sample_obj, ‘attr1’, ‘attr2’, ‘attr3’) print(values) # Output: [‘value1’, ‘value2’, None]
Extract attributes from a None object
values_none = extract_attrs_from_obj(None, ‘attr1’, ‘attr2’) print(values_none) # Output: []
NOTE
This function does not raise an exception for missing attributes; it simply returns None for those attributes.