Search code examples
pythonpython-3.xpython-descriptors

Is there a way to assign a descriptor to an instance attribute at runtime?


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.


Solution

  • No, the descriptor protocol only works when the descriptor object is a member of a class, this is stated int the HOWTO:

    To use the descriptor, it must be stored as a class variable in another class

    Or, from the data model docs:

    The following methods only apply when an instance of the class containing the method (a so-called descriptor class) appears in an owner class (the descriptor must be in either the owner’s class dictionary or in the class dictionary for one of its parents).