Lazy class property decorator

后端 未结 1 499
失恋的感觉
失恋的感觉 2021-01-21 11:59

I have one django model which needs to do some processing referring the custom user model.

I can\'t work with the class of this model at class loading time because the l

相关标签:
1条回答
  • 2021-01-21 12:43

    Pyramid framework has a very nice decorator called reify, but it only works at instance level, and you want class level, so let's modify it a bit

    class class_reify(object):
        def __init__(self, wrapped):
            self.wrapped = wrapped
            try:
                self.__doc__ = wrapped.__doc__
            except: # pragma: no cover
                pass
    
        # original sets the attributes on the instance
        # def __get__(self, inst, objtype=None):
        #    if inst is None:
        #        return self
        #    val = self.wrapped(inst)
        #    setattr(inst, self.wrapped.__name__, val)
        #    return val
    
        # ignore the instance, and just set them on the class
        # if called on a class, inst is None and objtype is the class
        # if called on an instance, inst is the instance, and objtype 
        # the class
        def __get__(self, inst, objtype=None):
            # ask the value from the wrapped object, giving it
            # our class
            val = self.wrapped(objtype)
    
            # and set the attribute directly to the class, thereby
            # avoiding the descriptor to be called multiple times
            setattr(objtype, self.wrapped.__name__, val)
    
            # and return the calculated value
            return val
    
    class Test(object):
        @class_reify
        def foo(cls):
            print("foo called for class", cls)
            return 42
    
    print(Test.foo)
    print(Test.foo)
    

    Run the program and it prints

    foo called for class <class '__main__.Test'>
    42
    42
    
    0 讨论(0)
提交回复
热议问题