Cool object trick
Alex Stapleton
alexs at advfn.com
Fri Dec 17 05:12:19 EST 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")
> 'Hello!'
>
> That's what getattr's for. ;) No need to go poking around in __dict__.
>
> Steve
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
mailing list