x.abc vs x['abc']
alex23
wuwei23 at gmail.com
Fri May 15 03:03:07 EDT 2009
On May 14, 5:49 am, Gunter Henriksen <gunterhenrik... at gmail.com>
wrote:
> Presuming it is very common to have objects created
> on the fly using some sort of external data
> definitions, is there an obvious common standard
> way to take a dict object and create an object
> whose attribute names are the keys from the dict?
I've always liked this approach, which I first saw in a post by Alex
Martelli:
>>> class Bunch(object):
... def __init__(self, **kwargs):
... self.__dict__.update(kwargs)
...
>>> b = Bunch(a=1,b=2,c=3)
>>> b.a
1
>>> b.b
2
>>> b.d = 22
>>> b.d
22
Elegant.
More information about the Python-list
mailing list