wiki:python/namespace object

Sometimes it can be useful to collect some stuff. For this a 'bundle' object can be just the thing:

>>> bundle = lambda: None
>>> bundle.foo = 'Really'
>>> bundle.bar = 'nice'
>>> print "{b.foo} {b.bar}, isn't it?".format(b=bundle)
Really nice, isn't it?

The simple but handy "collector of a bunch of named stuff" class

>>> class Bunch(object):
...     def __init__(self, **kwargs):
...         self.__dict__.update(kwargs)
... 
>>> bunch = Bunch(foo='Really', bar='nice')
>>> print "{b.foo} {b.bar}, isn't it?".format(b=bunch)
Really nice, isn't it?

Note: Since Python 3.3 there is a new type SimpleNamespace implemented

Last modified 3 years ago Last modified on May 12, 2016, 7:41:11 PM