[Tutor] Subclassing list

Luis N globophobe at gmail.com
Thu Jun 18 17:27:58 CEST 2009


I get an error "TypeError: 'rounding' is an invalid keyword argument
for this function" on my list subclass.

How might I subclass list without this error?

This is the code:

class SeriesList(list):
    def __new__(cls, *args, **kwargs):
        series_list = list.__new__(cls, *args)
        series_list.rounding = kwargs.get('rounding', None)
        return series_list

    def moving_average(self, function, period=10):
        index = 0
        window = []
        ma = []
        for i in self.__iter__():
            i = float(i)
            if is_not_nan(i):
                window.insert(0, i)
                if len(window) == period:
                    ma.append(function(window))
                    window.pop()
            else:
                ma.append(float('nan'))
        return round(ma, self.rounding)
---
Regards,

Luis


More information about the Tutor mailing list