替换功能以完成代码的代码

这是我编写的:

def statement(ticker,statement):
    if statement =='IS':
        temp='income-statement'
    elif statement =='BS':
        temp='balance-sheet-statement'
    else:
        temp='cash-flow-statement'
       
        df = tickers.temp(frequency='q') 
        return df

tickers=['AAPL','GOOG','TSLA']

我希望函数中的temp替换为df = tickers.temp(frequency ='q'),并在用股票代码替换股票代码时最终给我正确的财务报表。如果有人有类似的合适代码,请在下面将其发布

iCMS 回答:替换功能以完成代码的代码

我不确定您要实现什么。 所以我认为你有类似的东西

tickers.income-statement()

tickers.balance-sheet-statement()

... 并且您要根据该函数中的临时字符串调用该方法。 所以

myfunc = getattr(tickers,temp)

应该从实现的字符串中为您提供所需的功能。

本文链接:https://www.f2er.com/1976818.html

大家都在问