pre-PEP generic objects
Scott David Daniels
Scott.Daniels at Acm.Org
Thu Dec 2 15:26:31 EST 2004
Nick Craig-Wood wrote:
> Scott David Daniels <Scott.Daniels at Acm.Org> wrote:
>> You can simplify this:
>> class Hash(object):
>> def __init__(self, **kwargs):
>> for key,value in kwargs.items():
>> setattr(self, key, value)
>> __getitem__ = getattr
>> __setitem__ = setattr
> That doesn't work unfortunately...
>>>>h['a']
>
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> TypeError: getattr expected at least 2 arguments, got 1
>
> I'm not exactly sure why though!
I could have sworn I tested this, but I must have accidentally
tested h.a rather than h['a']. I don't know why it doesn't work either.
For example:
def gattr(*args): return getattr(*args)
def sattr(*args): return setattr(*args)
class Hash(object):
def __init__(self, **kwargs):
for key,value in kwargs.items():
setattr(self, key, value)
__getitem__ = gattr
__setitem__ = sattr
does work.
--Scott David Daniels
Scott.Daniels at Acm.Org
More information about the Python-list
mailing list