x.abc vs x['abc']

G

Gunter Henriksen

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:
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.
 
A

alex23

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:
.... def __init__(self, **kwargs):
.... self.__dict__.update(kwargs)
....22


Elegant.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top