pre-PEP generic objects

Scott David Daniels Scott.Daniels at Acm.Org
Tue Nov 30 14:09:28 EST 2004


Nick Craig-Wood wrote:
> class Hash:
>     def __init__(self, **kwargs):
>         for key,value in kwargs.items():
>             setattr(self, key, value)
>     def __getitem__(self, x):
>         return getattr(self, x)
>     def __setitem__(self, x, y):
>         setattr(self, x, y)

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

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list