Is it possible to access the ‘owner’ class inside a descriptor during the __init__ function of that descriptor, without passing it in manually as in this example?
class FooDescriptor(object):
def __init__(self, owner):
#do things to owner here
setattr(owner, 'bar_attribute', 'bar_value')
class BarClass(object):
foo_attribute = FooDescriptor(owner=BarClass)
One way to do something like that is with a metaclass. Just make sure it’s really what you want, and don’t just copy blindly if you don’t understand how it works.
If you need to pass additional arguments, you could use e.g. a tuple of
(class, args)in the place of the class, or makeFooDescriptora decorator that would return a class that takes only one argument in the ctor.