"Jonathan Claggett" <jcc.ugm at ix.netcom.com> wrote in message news:mailman.986578990.10259.python-list at python.org... > > >(Or, you could use a 'grab-bag instance object' -- it > >does not have to be a module-object; see for example > >http://www.activestate.com/ASPN/Python/Cookbook/Recipe/52308). > > How about improving the Bunch class to allow subscript access to its > attributes? This makes an instance of Bunch reminiscent of a Javascript > instance. > > >>> class Bunch: > ... def __init__(self, **kwds): > ... self.__dict__.update(kwds) > ... def __setitem__(self, key, value): > ... self.__dict__[key] = value > ... def __getitem__(self, key): > ... return self.__dict__[key] > ... > >>> ex = Bunch(salary=10000, rank=5) > >>> ex.rank > 5 > >>> ex['salary'] > 10000 > > > Or is this asking for trouble that I, an affirmed Python newbie, would not > be aware of? That's the trouble with languages, in addition to the syntax > rules there are always the undocumented 'good practice' rules :-) > The main idea of the Bunch is to make this unnecessary, but it seems like a reasonably logical extension apart from that. I would argue it isn't really an improvement, but I may be being purist. Of course, if you are *only* going to use subscripted access there's no point using a Bunch, since that's exactly what a dictionary gives you in the first place. regards Steve
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4