Cool object trick
alexs at advfn.com
Fri Dec 17 11:12:19 CET 2004
Steven Bethard wrote:
> Alex Stapleton wrote:
>> you are setting the variable name in your code (b.varA), not
>> generating the variable name in a string (var = "varA") (dictionary
>> key) at run-time and fetching it from the __dict__ like i was
>> attempting to describe.
> Ahh. Well if you just want to get an attribute, I don't see why you
> wouldn't do it the normal way:
> >>> b = Bunch(varA="Hello!")
> >>> getattr(b, "varA")
> That's what getattr's for. ;) No need to go poking around in __dict__.
Hmm true, (i had forgotten about getattr :/) in that case im indifferent
to Bunch() not that i really see why it's useful except for making code
look a bit nicer occasionaly.
More information about the Python-list