"Jonathan Claggett" <jcc.ugm at ix.netcom.com> writes: > >(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. Oh, it's easy indeed (just delegate __getitem__ to self.__dict__), but the resulting confusion between items and attributes seems a strange target to aim for. If you want a dictionary, it's easy to make one: > >>> ex = Bunch(salary=10000, rank=5) Becomes: ex = { 'salary': 1000, 'rank': 5 } > >>> ex.rank > 5 > >>> ex['salary'] > 10000 If ex was a dictionary, the latter syntax would be the only allowed one. What I'm missing is the added-value of allowing both... > 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 :-) Only applicable 'good practice' rule that I can think of here is not going out of your way to promote confusion. Maybe I'm missing something...? Alex _________________________________________________________ Do You Yahoo!? Get your free @yahoo.com address at http://mail.yahoo.com
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