This one is out of the Python Cookbook and credit goes to Alex Martelli if I'm not mistaken. This technique is useful when we would like to record data in some type of "forgiving" data structure.


We know we can accomplish this with a python dictionary, but what about when we want to nest an inconsistent data structure in another inconsistent data structure? This is when the bunch pattern is useful.


Example Program:



class Bunch:
def __init__(self, *args, **kwargs):
self.__dict__.update(kwargs)


struct = Bunch(type="flat", size="huge", family="chordata", genus=Bunch(level="medium", intensity="hot"), BOOL=True)


print struct
print struct.type
print struct.size
print struct.genus.level


Output:



<__main__ .bunch=".bunch" at="at" instance="instance">
flat
huge
medium



Pretty slick, huh? The beauty of this vignette is that it is highly extensible. The original thread where I stumbled upon this is located here.