x.abc vs x['abc']
Gunter Henriksen
gunterhenriksen at gmail.com
Wed May 13 15:49:05 EDT 2009
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 realize I can do something like:
>>> d = {"hello": "world"}
>>> x = type("", (object,), d)()
>>> x.hello
world
but that seems like an arcane way to do something
which would ideally be transparent... if there is
a function in the standard library, that would be
good, even if I have to import it. I guess there is
collections.namedtuple... that would not look much
prettier... but the main thing to me is for it to
be the same way everybody else does it. I do not
prefer the new object be a dict, but it would be ok.
More information about the Python-list
mailing list