generic object implementation

Peter Otten __peter__ at
Mon Nov 22 09:37:10 CET 2004

Steven Bethard wrote:

> So I'm trying to get a prototype implementation of the 'generic object'
> type.  (I'm currently calling it 'bunch', but only because I can't
> really think of anything better.)  I'd like some feedback on what

Me neither. I called my bunch equivalent Struct, but that name seems
ambiguous because of the struct module.

> methods it needs to support.  Ideally I'd keep this as minimal as
> possible...
> Remember that the goal of the 'generic object' type is to allow the
> programmer to make the design decision that attribute-style access is
> more appropriate than []-style access.  Given that, my feeling is that
> the 'generic object' type should *not* support __(get|set|del)item__ ,
> though I'm still undecided on __len__, __iter__, __contains__, items,
> keys, values, etc.

Please, no. (except __iter__(), maybe)

> Here's what I have currently:
> import operator as _operator
> class bunch(object):
>      def __init__(self, **kwds):
>          self.__dict__.update(kwds)
>      def __eq__(self, other):

Maybe just
           return (type(self) == type(other) 
                   and self.__dict__ == other.__dict__)

as you won't get properties right anyway. 
Not sure whether that should be enforced, but subclassing bunch doesn't seem
a good idea to me.
>          if not isinstance(other, bunch):
>              return False
>          attrs = set(self.__dict__)
>          if attrs != set(other.__dict__):
>              return False
>          for attr in attrs:
>              if not getattr(self, attr) == getattr(other, attr):
>                  return False
>          return True
>      def __repr__(self):
>          return '%s(%s)' % (self.__class__.__name__,
>                             ', '.join('%s=%r' % (k, v)
>                                       for k, v in self.__dict__.items()))
>      def update(self, other):
>          self.__dict__.update(other.__dict__)

I don't think update() is necessary, but if so, I'd rather have (untested)

       def update(self, *other, **kw):
           if other:
               if len(other) != 1: 
                   raise TypeError
               other = other[0]
               if isinstance(other, bunch):
                   other = other.__dict__

i. e. something matching 2.4's dict.update() functionality as closely as
possible (and appropriate).

>      @classmethod
>      def frommapping(cls, mapping,
>                      getkeys=iter, getitem=_operator.getitem):
>          result = bunch()

Substitute bunch with cls throughout. 
Should there also be a getpairs argument that would yield (key, value)

>          for key in getkeys(mapping):
>              value = getitem(mapping, key)
>              try:
>                  value = bunch.frommapping(value)
>              except TypeError:
>                  pass
>              setattr(result, key, value)
>          return result

Seeing how many aspects are to consider with such a simple thing as a bunch,
it would really be a benefit if one standard approach were available to
substitute all the ad hoc solutions currently out there in the wild.
Good luck with your effort.


More information about the Python-list mailing list