Often, it would be convenient to access dict entries with my_dict.my_key
instead of my_dict['my_key']
.
So I came up with the following solution:
class ddict(dict):
def __init__(self, **kwargs):
dict.__init__(self, **kwargs)
for (k, v) in kwargs.items():
self.__dict__[k] = v
You can use it as:
d = ddict(a = 1, b = 2)
print(d.a)
print(d.b)
Is that approach safe or will it bite me at some point? Is there maybe even a built-in approach?
(Why do I want that? Easier typing and looks better than a dict or an explicitly defined class, but that should not be the topic here, since it's a matter of taste and situation. And it's iterable.)