python - 惰性类属性装饰器

标签 python django decorator lazy-evaluation class-attributes

我有一个 Django 模型,需要引用自定义用户模型进行一些处理。

我无法在类加载时使用此模型的类,因为类的加载顺序未知。

所以我需要在运行时添加一些类属性,目前我将它们添加到 __init____new__ 中,例如:

def __new__(cls, *args, **kwargs):
    # hack to avoid INSTALLED_APPS initialization conflicts.
    # get_user_model() can't be called from this module at class loading time,
    # so some class attributes must be added later.
    # Metaclasses could me more appropiate but I don't want to override
    # dango's metaclasses.
    if not hasattr(cls, '_reverse_field_name_to_user'):
        cls._find_reverse_field_name_to_user()
    return Group.__new__(cls, *args, **kwargs)

它有效,但看起来很糟糕,所以我考虑过为这些属性使用类似 @lazyclassproperty 的东西。

我找到了几个 @classproperty@lazyproperty 装饰器,但没有一个同时适用于这两个装饰器,而且我不知道如何自己编写一个。

问题:我如何编写这样的装饰器?或为我当前的愚蠢实现建议另一种更清洁的替代方案。

最佳答案

Pyramid 框架有一个非常好的装饰器,叫做 reify , 但它只适用于实例级别,而你想要类级别,所以让我们稍微修改一下

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)

运行程序并打印

foo called for class <class '__main__.Test'>
42
42

关于python - 惰性类属性装饰器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18289871/

相关文章:

python - 为什么我的 python egg 不起作用? - 根本找不到分布

python - np.ascontiguousarray 与 np.asarray 与 Cython

python - 升级到 Django 2.1 后不再发送 500 错误消息

python - 从我的计算机python中提取子网掩码

python - 从 BLOB mysql python 写入文件

javascript - 弹出窗口的显示 id 按钮不起作用

python - 将 python (django) 数据序列化为 javascript

python - 我们可以在函数调用时应用 python 装饰器吗?

python - 将文档字符串放在 Python 属性上的正确方法是什么?

python - 实现装饰器,它将用另一个实现替换一个类