Let’s say I have the following class
class Parent(object):
Options = {
'option1': 'value1',
'option2': 'value2'
}
And a subclass called Child
class Child(Parent):
Options = Parent.Options.copy()
Options.update({
'option2': 'value2',
'option3': 'value3'
})
I want to be able to override or add options in the child class. The solution I’m using works. But I’m sure there is a better way of doing it.
EDIT
I don’t want to add options as class attributes because I have other class attributes that aren’t options and I prefer to keep all options in one place. This is just a simple example, the actual code is more complicated than that.
One way would be to use keyword arguments to dict to specify additional keys:
If you want to get fancier, then using the descriptor protocol you can create a proxy object that will encapsulate the lookup. (just walk the owner.__mro__ from the owner attribute to the __get__(self, instance, owner) method). Or even fancier, crossing into the probably-not-a-good-idea territory, metaclasses/class decorators.