使用if语句Python更改函数的结构 [英] Changing the structure of a function with if statement Python

查看:37
本文介绍了使用if语句Python更改函数的结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

函数a可以采用 client.LinearKline.LinearKline_get()的形式,也可以采用 client.Kline.Kline_get()的形式.我该如何使a和b模块化,以使这两个选项都可以正常工作,下面的功能实际上是行不通的.

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(). How could I make a and b modular so that both options could work the function below does not really work.

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

client.a.b()

推荐答案

您也可以为此使用 getattr .

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

getattr(getattr(client, a), b)()

这篇关于使用if语句Python更改函数的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆