Why is the Descriptor not getting called when defined as instance attribute?

前端 未结 1 1718
名媛妹妹
名媛妹妹 2020-12-06 11:04

When I make the \"data\" variable a class variable, the following works, but when I make it an object variable, the descriptor is not called. Please help.

cl         


        
相关标签:
1条回答
  • 2020-12-06 11:35

    That's because descriptors should only be defined as class attributes, not instance attributes:

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

    To make descriptor work with instance attributes as well you need to override the __getattribute__ method of BusinessLogic.(Haven't tested this thoroughly, but works fine for your case):

    def __getattribute__(self, attr):
            obj = object.__getattribute__(self, attr)
            if hasattr(obj, '__get__'):
                return obj.__get__(self, type(self))
            return obj
    

    In case you've a data descriptor then you need to handle the __setattr__ part as well.

    def __setattr__(self, attr, val):
        try:
            obj = object.__getattribute__(self, attr)
        except AttributeError:
            # This will be raised if we are setting the attribute for the first time
            # i.e inside `__init__` in your case.
            object.__setattr__(self, attr, val)
        else:
            if hasattr(obj, '__set__'):
                obj.__set__(self, val)
            else:
                object.__setattr__(self, attr, val)
    
    0 讨论(0)
提交回复
热议问题