2

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.

shadowtalker
  • 12,529
  • 3
  • 53
  • 96
  • Are you perhaps looking for "mixins"? Maybe something like [this](https://stackoverflow.com/a/20022860/4799172). I'm much less-sure about "at runtime" though, on that suggestion – roganjosh Feb 01 '21 at 20:37
  • @roganjosh I am specifically asking if this is possible to achieve at runtime, as I described in the question. If the answer is "no", then the answer is "no". – shadowtalker Feb 01 '21 at 22:42
  • Of course *you can* assign a descriptor to an instance attirbute, but *that won't be considered by the descriptor protocol* – juanpa.arrivillaga Jul 02 '21 at 03:31

1 Answers1

1

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).

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172