Jack Diederich wrote: > On Thu, May 04, 2006 at 02:08:30PM -0400, Steven Watanabe wrote: > > I'm trying to do something like this in Python 2.4.3: > > > > class NamedSet(set): > > def __init__(self, items=(), name=''): > > set.__init__(self, items) > > self.name = name > > > > class NamedList(list): > > def __init__(self, items=(), name=''): > > list.__init__(self, items) > > self.name = name > > > > I can do: > > > > >>> mylist = NamedList(name='foo') > > > > but I can't do: > > > > >>> myset = NamedSet(name='bar') > > TypeError: set() does not take keyword arguments > > > > How come? How would I achieve what I'm trying to do? > > setobject.c checks for keyword arguments in it's __new__ instead > of its __init__. I can't think of a good reason other to enforce > inheriters to be maximally set-like. We're all adults here so > I'd call it a bug. bufferobect, rangeobject, and sliceobject all > do this too, but classmethod and staticmethod both check in tp_init. > Go figure. > > As a work around use a function to make the set-alike. > > class NamedSet(set): pass > > def make_namedset(vals, name): > ob = NamedSet(vals) > ob.name = name > return ob > > Then make_namedset as a constructor in place of NamedSet(vals, name)
Or use this work around: class NamedSet(set): def __new__(cls, iterable=(), name=""): return super(NamedSet, cls).__new__(cls) def __init__(self, iterable=(), name=""): super(NamedSet, self).__init__(iterable) self.name = name -- http://mail.python.org/mailman/listinfo/python-list