【问题标题】:Error when calling function from another function [duplicate]从另一个函数调用函数时出错[重复]
【发布时间】:2019-10-14 11:49:33
【问题描述】:

我在从另一个函数内部调用一个函数时遇到了一些问题

这是我的代码:

supported_timeframes = ['1m', '5m', '1h', '1d']
supported_indicators = ['SMA', ... ]
supported_sources = ['open', 'close']

indicator_values = {}


class Indicator:

    def __init__(self):
        self.indictor = ''      # indicator 
        self.timeframe = ''     # timeframe
        self.period = 0         # look-back period
        self.source = ''        # open / close

    def error(self, indicator, timeframe, period, source):
        if timeframe not in supported_timeframes:
            return 'timeframe not supported --', 'supported 
            timeframes:', supported_indicators
        if indicator not in supported_indicators:
            return 'indicator not found --', 'supported indicators:', 
            supported_indicators
        if source not in supported_sources:
            return 'source is not -- open -- or -- close'
    else:
        pass

    def SMA(self, indicator, timeframe, period, source):
        error()

        # TODO get candle data from timeframe
        # TODO calc requested indicator from candle data and append to 
        # indicator_values dict

        # return indicator
        for indicator in indicator_values:
            return indicator

我有函数错误首先检查是否在参数中输入了错误的值。

然后我想在 def SMA(...) 函数中调用该函数,因为我将拥有更多函数,每个函数都计算一个类似于 SMA 的指标,所以为了简洁起见,我试图在每个函数中调用 error()而不是每次都复制和粘贴代码。

但是,在执行此操作时,我在 def SMA(...) 中收到一条错误消息,指出 error() 未定义。

如果您能帮助我,请提前非常感谢您!

【问题讨论】:

    标签: python function class


    【解决方案1】:

    除非我遗漏了什么,否则你只需要在你调用 error() 的地方添加 self

    def SMA(self, indicator, timeframe, period, source):
            self.error()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-21
      • 1970-01-01
      • 1970-01-01
      • 2017-03-07
      相关资源
      最近更新 更多