使用 boto3 命令作为字符串

use boto3 command as a string

我们可以使用 boto3 包从 mTurk 帐户获取数据(或在 AWS 中执行任何其他操作)。例如:

client = boto3.client('mturk')
balance = client.get_account_balance()

有没有办法将 boto3 命令用作字符串?类似于:

balance = client.get_command('get_account_balance')
当然,

get_command完全只是为了说明目的。

使用getattr内置函数:

getattr(client, 'get_account_balance')()

您可以使用内置 getattr:

def get_command(client, command):
    return getattr(client, command)()