
This is in principle doable with metaclasses (like everything else :-D)... Here's what I whipped up in the console:
class MagicDict(dict): ... def __setitem__(self, key, value): ... print("Binding key: {} to value: {}".format(key, value)) ... if hasattr(value, "__bind__"): ... value.__bind__(key) ... super().__setitem__(key, value) ... class Bindable: ... def __bind__(self, key): ... print("{} was bound to {}".format(self, key)) ... class MetaBinding(type): ... @classmethod ... def __prepare__(metacls, name, bases): ... return MagicDict() ... class BindingReports(metaclass=MetaBinding): ... a = 1 ... b = Bindable() ... c = "blah" ... Binding key: __module__ to value: __main__ Binding key: a to value: 1 Binding key: b to value: <__main__.Bindable object at 0x100603b50> <__main__.Bindable object at 0x100603b50> was bound to b Binding key: c to value: blah
Not sure how useful this is. I don't like using the term "class" for things where you're not really trying to bundle together methods or create a type, just change the way values get bound. -- Carl Johnson