I'm working on a library that provides (1) a class decorator to automatically store certain attributes to a file and (2) a function to patch the same behavior into existing instances.
Implementation of the class decorator is fairly simple:
def store_instances(path, attrs):
def decorator(cls):
class Decorated(cls, StorageBehavior):
_path = path
_attrs = attrs
return Decorated
return decorator
but for the function, what's the best way to add behavior from a base class into an existing object? Searching for "dynamically changing base classes" yields this question, but the answers are essentially "don't do that and/or it's not possible". Is there a better way to this?