[Tutor] Subclassing list

Kent Johnson kent37 at tds.net
Fri Jun 19 04:56:53 CEST 2009

On Thu, Jun 18, 2009 at 9:48 PM, Luis N<globophobe at gmail.com> 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

> 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

I think you need to define SeriesList.__init__(self, rounding=None).
This function should assign self.round, then __new__() can be just
   def __new__(cls, *args, **kwargs):
       series_list = list.__new__(cls, *args)
       return series_list


More information about the Tutor mailing list