Why does built-in set not take keyword arguments?
Jack Diederich
jack at performancedrivers.com
Thu May 4 18:39:06 EDT 2006
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)
-Jack
More information about the Python-list
mailing list