1

There is the function a could either be in the form of client.LinearKline.LinearKline_get() or in the form of client.Kline.Kline_get(). a and b, (first_run) however the a,(second_run) does not work. c just combines a and b and is simplifies it. My previous post: issue . How would I be able to make 'c' work with getattr.

choice= 1

if choice == 1:
    a = "LinearKline"
    b = "LinearKline_get"
    c = "LinearKline.LinearKline_get"
else:
    a = "Kline"
    b = "Kline_get"
    c = "Kline.Kline_get"

first_run =getattr(getattr(client, a), b)()
second_run= getattr(client, c)()
tony selcuk
  • 709
  • 3
  • 11

1 Answers1

2

Don't do it that way. It's just the wrong path, and it will be unmaintainable in the long run..

def getChoice(client):
    if choice == 1:
        return client.LinearKline.LinearKline_get
    else:
        return client.Kline.Kline_get

first_run = getChoice(client)()
second_run= getChoice(client)()
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • 1
    There are times when OP's general approach will be the right thing to do, and the process of creating a minimal reproducible example can obscure the justification for doing it. That said, it's worth pointing out that it's possible to store e.g. `x = client.Kline.Kline_get` and then have `first_run = x()`. – Karl Knechtel Apr 29 '21 at 21:42