This question was asked in 2016, but was not answered: Can a descriptor be assigned to an instance attribute instead of a class attribute?
I have a basic descriptor
class MyThing:
def __set_name__(self, owner, name):
print(f'Assigned to {name} on {owner}')
self.name = name
def __get__(self, obj, typ):
print(f'Getting {self.name} on {obj}')
return getattr(obj, self.name)
def __set__(self, obj, val):
print(f'Setting {self.name} on {obj} to {val}')
setattr(obj, self.name, val)
Is it possible to assign an instance of this descriptor to some other class instance at runtime, and have it behave in accordance with the descriptor protocol?
The naive approach fails:
foo.thing = MyThing()
foo.thing
does not behave like a descriptor, it just behaves like a typical instance attribute.
This is not an XY problem. I am specifically asking for an answer to the question as I have stated it.