If you want to dynamically call a method on an object in Python, you can use the getattr()
function. It allows you to get the value of a named attribute of an object. If the name is a method, you can then call it.
Here is an example:
def dynamic_call(sf, action_cmd, payload):
# Get the method from 'sf' based on 'action_cmd'
method_to_call = getattr(sf, action_cmd)
# Call the method and pass 'payload' as a parameter
response = method_to_call(**payload)
return response
This function takes your object (sf
), the name of the method to call as a string (action_cmd
), and a dictionary of parameters to pass to the method (payload
). It uses getattr()
to get the method from sf
, then calls it using the parameters in payload
.
You could use this function like this:
payload = {'maxResults': 2}
action_cmd = 'list_state_machines'
response = dynamic_call(sf, action_cmd, payload)
Here, payload
is a dictionary where the keys are the names of the parameters for the method you want to call and the values are the values for those parameters.
This will work as long as the methods you're trying to call always take their parameters as keyword arguments. If some methods require positional arguments, you'll need a more complex solution.