python - Python:动态实例化
问题描述
我需要使用对象变量来减少代码行和复杂性。这是我将要使用的代码:
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"
推荐阅读
- spring - 如何禁用弹簧启动执行器弹性搜索健康请求?
- symfony - 当我尝试创建新的时,php bin/console make:crud 中有错误
- python - 如何使用 python 在 Windows 10 中进行并行处理
- python - 根据条件语句在熊猫数据框中生成新列
- bash - 如何使用 cat 命令从顶部读取文件?
- javascript - 如何将 JQuery 代码转换为 JavaScript?
- python - 在 Python 中以 1 到 3 范围内的随机重复间隔生成 2 个变量的列表
- node.js - Actions on Google 检查可用手机
- excel - VBA根据excel条件从两个word文档中复制文本
- android - AccessibilityService中的Android onAccessibilityEvent没有被调用