0

I have a sample dictionary.

>>> my_dict = {'name': 'James', 'age': 10}

I want to get attributes from dictionary as following,

>>> my_dict.name
James
>>> my_dict.age
10

How can I override dict meta methods for this ? I have dictionary consisting large number of key, values. I just want convert it to some object so that it can work as normal dictionary as well as getting attribute as explained above.

Sanket Sudake
  • 763
  • 6
  • 18

1 Answers1

0

You could create a namedtuple:

>>> from collections import namedtuple
>>> my_dict = {'name': 'James', 'age': 10}
>>> my_dotted = namedtuple('GenericDict', my_dict.keys())(**my_dict)
>>> my_dotted
GenericDict(name='James', age=10)
>>> my_dotted.name
'James'
Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47