Ian Bicking wrote:
class bunch(object): def __init__(self, **kw): for name, value in kw.items(): # IMPORTANT! This is subclass friendly: updating __dict__ # is not! setattr(self, name, value)
Good point about being subclass friendly... I wonder if there's an easy way of doing what update does though... Update (and therefore __init__) allows you to pass in a Bunch, dict, (key, value) sequence or keyword arguments by taking advantage of dict's update method. Is there a clean way of supporting all these variants using setattr?
class bunch(object): def __init__(self, __seq=None, **kw): if __seq is not None: if hasattr(__seq, 'keys'): for key in __seq: setattr(self, key, __seq[key]) else: for name, value in __seq: setattr(self, name, value) for name, value in kw.items(): setattr(self, name, value)
That should match dict.update, at least from the 2.4 help(dict.update). I'm not sure that will work for updating from a bunch object; also, bunch objects could have a 'keys' attribute without being dictionaries. Do you get attributes from non-iterables through their __dict__? I don't care for that at all. Are bunch objects iterable?
-- Ian Bicking / [EMAIL PROTECTED] / http://blog.ianbicking.org -- http://mail.python.org/mailman/listinfo/python-list