Can I get a reference to the 'owner' class during the __init__ method of a descriptor?

前端 未结 2 1932
天涯浪人
天涯浪人 2021-01-24 13:51

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?

2条回答
  •  伪装坚强ぢ
    2021-01-24 14:26

    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.

    class Descriptor(object):
        pass
    
    class Meta(type):
        def __new__(cls, name, bases, attrs):
            obj = type.__new__(cls, name, bases, attrs)
            # obj is now a type instance
    
            # this loop looks for Descriptor subclasses
            # and instantiates them, passing the type as the first argument
            for name, attr in attrs.iteritems():
                if isinstance(attr, type) and issubclass(attr, Descriptor):
                    setattr(obj, name, attr(obj))
    
            return obj
    
    class FooDescriptor(Descriptor):
        def __init__(self, owner):
            owner.foo = 42
    
    class BarClass(object):
        __metaclass__ = Meta
        foo_attribute = FooDescriptor # will be instantiated by the metaclass
    
    print BarClass.foo
    

    If you need to pass additional arguments, you could use e.g. a tuple of (class, args) in the place of the class, or make FooDescriptor a decorator that would return a class that takes only one argument in the ctor.

提交回复
热议问题