Cool object trick

Alex Stapleton alexs at advfn.com
Fri Dec 17 04:47:43 EST 2004


Steven Bethard wrote:
> 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

thats nothing like what i described.

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.





More information about the Python-list mailing list