[Python-ideas] question - 'bag' type

George Sakkis george.sakkis at gmail.com
Thu Apr 15 16:31:00 CEST 2010


On Thu, Apr 15, 2010 at 1:43 PM, Michael Foord <fuzzyman at gmail.com> wrote:
>
>
> On 15 April 2010 08:02, alex23 <wuwei23 at gmail.com> wrote:
>>
>> On Apr 15, 3:02 pm, "C. Titus Brown" <c... at msu.edu> wrote:
>> >    b = bag(foo=bar, baz=bif)
>> >
>> >    assert b.foo == bar
>> >    assert b.bz == bif
>>
>> I've always known this as a 'bunch' after Alex Martelli's recipe[1].
>> It's handy, but at 3 lines of code not an urgent candidate (IMO) for
>> stdlib:
>>
>>  class Bunch:
>>    def __init__(self, **kwds):
>>      self.__dict__.update(kwds)
>
>
> Yeah, we've used this class a tonne - especially for testing where you just
> need a quick object with a bunch of attributes. I think we added a repr as
> well. It would certainly be a nice harmless addition to the collections
> module.
>
> Michael

In the past I've found useful a hybrid of the 'bunch' and the namedtuple[1].

George

[1] http://code.activestate.com/recipes/576555-records/



More information about the Python-ideas mailing list