27 lines
753 B
Python
27 lines
753 B
Python
class Bunch:
|
|
def __init__(self, **kwds):
|
|
self.__dict__.update(kwds)
|
|
|
|
# that's it! Now, you can create a Bunch
|
|
# whenever you want to group a few variables:
|
|
|
|
point = Bunch(datum=y, squared=y*y, coord=x)
|
|
|
|
# and of course you can read/write the named
|
|
# attributes you just created, add others, del
|
|
# some of them, etc, etc:
|
|
if point.squared > threshold:
|
|
point.isok = 1
|
|
|
|
# Even shorter Bunch, Doug Hudgeon, 2001/08/29
|
|
class Bunch:
|
|
__init__ = lambda self, **kw: setattr(self, '__dict__', kw)
|
|
|
|
|
|
# When python 2.2 permits the above dynamic __dict__ this could be
|
|
# be something like:
|
|
|
|
class Bunch(list):
|
|
def __init__(*args, **kw):
|
|
self[:] = list(args)
|
|
setattr(self, '__dict__', kw) |