Python:动态实例化

Python:动态实例化,python,dynamic,instantiation,Python,Dynamic,Instantiation,我需要使用一个对象变量来减少代码行和复杂性。下面是我将要使用的代码: exchange = ccxt.binance({ 'apiKey': 'YOUR_API_KEY', 'secret': 'YOUR_SECRET', 'enableRateLimit': True, }) 我需要使binance部分动态,因为在这种情况下可能有几百种不同的东西。有没有一个简单的方法 提前感谢。如果您想每次调用不同的方法,可以使用: try:

我需要使用一个对象变量来减少代码行和复杂性。下面是我将要使用的代码:

exchange = ccxt.binance({
        'apiKey': 'YOUR_API_KEY',
        'secret': 'YOUR_SECRET',
        'enableRateLimit': True,
    })
我需要使
binance
部分动态,因为在这种情况下可能有几百种不同的东西。有没有一个简单的方法


提前感谢。

如果您想每次调用不同的方法,可以使用:

try:
    # get a method and choose it's name runtime
    yourMethod = getattr(ccxt, 'binance')
    # call it
    yourMethod({
        'apiKey': 'YOUR_API_KEY',
        'secret': 'YOUR_SECRET',
        'enableRateLimit': True,
    })
catch AttributeError:
    print "method with that name doesn't exist"

什么是数据结构
ccxt
?您想选择调用哪个方法名吗?在其他编程语言(如PHP)中,这是一个非常常见且简单的操作:
$method='mymethod'$类->{$method}()