Python:动态实例化
Python: Dynamic instantiation
我需要使用对象变量来减少代码行和复杂性。这是我将要使用的代码:
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"
我需要使用对象变量来减少代码行和复杂性。这是我将要使用的代码:
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"