Cool object trick

Steven Bethard steven.bethard at gmail.com
Fri Dec 17 10:39:07 CET 2004


Alex Stapleton wrote:
> you can't do
> 
> var = "varA"
> obj = struct(varA = "Hello")
> print obj.var
> 
> and expect it to say Hello to you.

The Bunch object from the PEP can take parameters in the same way that 
dict() and dict.update() can, so this behavior can be supported like:

 >>> b = Bunch({"varA":"Hello!"})
 >>> b.varA
'Hello!'

or

 >>> b = Bunch([("varA", "Hello!")])
 >>> b.varA
'Hello!'

Steve



More information about the Python-list mailing list