Michael Michael - 5 days ago 6
Python Question

How to make a dict with access by my_dict.my_key?

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.)

Answer

A better and safer way would be to use _getattr_ (which is what Python calls when you use the . notation, but do note that this approach only allows for string keys (not integers, floats or arbitrary objects):

class ddict(dict):
    def __getattr__(self, item):
        return self[item]

d = ddict()
d['a'] = 1
print(d.a)
>> 1

You may also override __setattr__ so you can assign with the . notation as well.

class ddict(dict):
    def __getattr__(self, item):
        return self[item]

    def __setattr__(self, key, value):
        self[key] = value

d = ddict()
d.a = 1
print(d.a)
>> 1
Comments