Adr*_*ián 2 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)
Run Code Online (Sandbox Code Playgroud)
它可以工作,但是看起来很可怕,所以我考虑过@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)
Run Code Online (Sandbox Code Playgroud)
运行程序并打印
foo called for class <class '__main__.Test'>
42
42
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1293 次 |
| 最近记录: |