[Tutor] Subclassing list

Luis N globophobe at gmail.com
Fri Jun 19 03:48:03 CEST 2009


On Fri, Jun 19, 2009 at 12:56 AM, bob gailer <bgailer at gmail.com> wrote:
>
> Luis N wrote:
>>
>> 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)
>> ---
>>
>
> I copied and ran the above. It gives me no errors.
>
> Of course all it is is a class definition.
>
> Is there more to the code?
>
> And please post the traceback so we have more information!
>
> --
> Bob Gailer
> Chapel Hill NC
> 919-636-4239

This is the traceback. I want to let SeriesList know how to round
moving average series derived from itself.

In [121]: s = SeriesList(rounding=1)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/Users/Luis/Documents/Programming/speculation/<ipython console> in <module>()

TypeError: 'rounding' is an invalid keyword argument for this function


Thank you for your help,

Luis


More information about the Tutor mailing list