When using traitlets.config, you can mirror your Python class inheritance hierarchy in your configuration. If a subclass inherits from a parent class that uses config=True for its traits, the subclass will automatically pick up the configuration settings applied to the parent class.
To implement this:
- Define your classes inheriting from
Configurable. - Set
config=True on the traits you want to be configurable. - In your configuration file, use the class name as a key to set values. Subclasses will inherit these values unless they are explicitly overridden.
from traitlets.config import Application, Configurable
from traitlets import Integer, Float, Unicode, Bool
class Foo(Configurable):
name = Unicode("fooname", config=True)
value = Float(100.0, config=True)
class Bar(Foo):
name = Unicode("barname", config=True)
othervalue = Integer(0, config=True)
# In your configuration file:
# c = get_config()
# c.Foo.name = "bestname"
# c.Bar.othervalue = 10
# Resulting behavior:
# - Bar().name will be "bestname" (inherited from Foo config)
# - Bar().value will be 100.0 (class default)
# - Bar().othervalue will be 10 (explicit Bar config)