invoke()
Call a method on each item in an array.
Implementation
Args: array: The array of objects func_name: The method name to call on each object *args: Arguments to pass to the method Returns: A list of method call results
Example
array = [1, 2, 3]
invoke(array, 'sqrt')
Expected output: [1.0, 1.4142135623730951, 1.7320508075688772]
Source Code
def invoke(array: List[Any], func_name: str, *args) -> List[Any]:
return [
getattr(item, func_name)(*args) if hasattr(item, func_name) else None
for item in array
]