简体   繁体   English

传递类的函数作为参数以在类列表上调用它

[英]Passing a function of a class as argument to call it on a list of classes

I'm trying to call functions of a list of classes of the same type using a function. 我正在尝试使用一个函数来调用相同类型的类列表。 I want this, because I have to loop over several objects and do the same operation on each of them, and I have several differing functions running such a for loop. 我想要这个,因为我必须遍历几个对象并对每个对象执行相同的操作,并且我有几个不同的函数正在运行这样的for循环。

Given this: 鉴于这种:

def make_timeSeries(symbol):
    return pandas.DataFrame(symbol) # Pseudo-code; But the return value is a pd.DataFrame Object nonetheless.

Class TimeSeries:
    def __init__(self, symbol):
        self.series = make_timeSeries(symbol)

    def get_y_of_day(self, day, _type):
    # Returns a y value for a given X (date) value from a timeline.
        if isinstance(day, str):
            day = funcs.convert_str_to_datetime(day)
        return self.series[(self.series['Date'] >= day) &
                           (self.series['Type'] ==
                            _type)].head(1)['Value'].values[0]

Class Market:
    def __init__(self, symbols):
        self.instruments = {}
        for symbol in symbols:
            self.instruments[symbol] = TimeSeries(make_timeSeries(symbol))

    def _market_day_apply_funcs(self, func, func_list=None, *args):
    # calls a given class function of TimeSeries for each TimeSeries stored in self.instruments
        ys = []
        for symbol in self.instruments:
            ys.append(self.instruments[symbol].func(args))
        if func_list is None:
            return ys
        else:
            return func_list(ys)

    def market_ys_of_day(self, day, _type):
    # Masking function, returns a list of all instruments at the given X (Date) value.
        return self._market_day_apply_funcs(get_y_of_day, day, _type)

I'm calling it then as follows: 我这样称呼它如下:

print(market.market_ys_of_day('2016-2-1', 'vola'))

This, however raises a NameError : 但是,这引发了NameError

Traceback (most recent call last):
  File "/markets.py", line 73, in <module>
    print(market.market_ys_of_day('2016-2-1', 'vola'))
  File "/markets.py", line 38, in market_ys_of_day
    return self._market_day_apply_funcs(get_y_of_day, day, _type)
NameError: name 'get_y_of_day' is not defined

I'm assuming the way to resolve this Error would be to pass the entirety of self.instruments[symbol].func , but that would defeat the purpose of the function itself, which is to not have a repeating for loop in each market_day_* function. 我假设解决此错误的方法将是传递整个self.instruments[symbol].func ,但这将self.instruments[symbol].func函数本身的目的,即在每个market_day_*中都没有重复的for循环。功能。

Anyone know a solution for this? 有人知道解决方案吗?

Edit It may be of interest to state that my IDe highlights the func argument as not used in the function's scope. 编辑可能有必要指出,我的IDe突出显示了func范围中未使用的func参数。 Extra credit if anyone can explain that bit as well. 如果有人也可以解释这一点,则可以给予额外的荣誉。

So far as I can see: 据我所知:

ys.append(self.instruments[symbol].func(args))

should be: 应该:

ys.append(func(args))

and

return self._market_day_apply_funcs(get_y_of_day, day, _type)

should be: 应该:

return self._market_day_apply_funcs(TimeSeries.get_y_of_day, day, _type)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM