1

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.

asked Aug 1, 2018 at 15:40
3
  • What data structure is ccxt? Commented Aug 1, 2018 at 15:42
  • do you want to choose which method name you call? Commented Aug 1, 2018 at 15:49
  • In other programming languages, like PHP, this is a very common and easy thing to do: $method = 'mymethod'; $class->{$method}(); Commented Jan 24, 2020 at 15:29

1 Answer 1

2

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"
answered Aug 1, 2018 at 15:57
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.