Cool object trick

Steven Bethard steven.bethard at gmail.com
Fri Dec 17 04:53:52 EST 2004


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



More information about the Python-list mailing list