xendi
xendi

Reputation: 2522

Python: Dynamic instantiation

I need to make the usage of an object variable to cut way down on lines of code and complexity. Here's the code I'm about to use:

exchange = ccxt.binance({
        'apiKey': 'YOUR_API_KEY',
        'secret': 'YOUR_SECRET',
        'enableRateLimit': True,
    })

I need to make the binance part dynamic since there's probably a few hundred different things that could be in this case. Is there an easy way to do that?

Thanks in advance.

Upvotes: 1

Views: 99

Answers (1)

nio
nio

Reputation: 5289

If you want to call each time a different method, you can use:

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"

Upvotes: 2

Related Questions