Python 从另一个函数调用函数时出错
我在从一个函数内部调用另一个函数时遇到一些问题 以下是我的代码:Python 从另一个函数调用函数时出错,python,function,class,Python,Function,Class,我在从一个函数内部调用另一个函数时遇到一些问题 以下是我的代码: supported_timeframes = ['1m', '5m', '1h', '1d'] supported_indicators = ['SMA', ... ] supported_sources = ['open', 'close'] indicator_values = {} class Indicator: def __init__(self): self.indictor = ''
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
我首先使用函数error来检查参数中是否输入了错误的值
然后我想在defsma(…)
函数中调用该函数,因为我将有更多的函数,每个函数都计算一个类似于SMA的指标,所以为了保持简洁,我尝试在每个函数中调用error()
,而不是每次都复制和粘贴代码
但是,在执行此操作时,我在defsma(…)
中收到一条错误消息,表示error()
未定义
如果你能帮我的话,我提前非常感谢你 除非我遗漏了什么,否则您只需要在调用error()的地方添加self
def SMA(self, indicator, timeframe, period, source):
self.error()