I’m looking for a clean, simple way to update class-level dictionaries, which are inherited from base classes. For example:
class Foo(object):
adict = {'a' : 1}
class Bar(Foo):
adict.update({'b' : 2}) # this errors out since it can't find adict
so that:
Foo.adict == {'a' : 1}
Bar.adict == {'a' : 1, 'b' : 2}
I’d prefer to not use instances here, and if possible not use class methods either.
Note that even if that worked, you’d update the same dictionary instead of creating a new one (so
Foo.adict is Bar.adictand thusFoo.adict == Bar.adict).In any case, the simplest way is to explicitly refer to the parent class’s dict (and copy it, see above):