【发布时间】:2017-11-19 11:07:01
【问题描述】:
在我的课堂上我写了这个方法:
def ts_prep(self, test_id):
ltouple = ()
tab_lib = temp_library.objects.filter(main_id=test_id)
l1 = ["Settings", ""]
ltouple += (l1,)
if tab_lib.count() == 0: ltouple = (l1, ["", ""])
l = []
for r in tab_lib.iterator():
l.append(str(r.l_type))
l.append(str(r.l_val))
ltouple += (l,)
l = []
tslist = [x for x in ltouple]
return tslist
如何为我的方法创建一个装饰器来执行 if tab_lib 检查,以便在所有进行相同检查的方法中添加它?
我试试
def p_decorate(func):
def func_wrapper(test_id):
tab_lib = temp_library.objects.filter(main_id=test_id)
l1 = ["Settings", ""]
ltouple += (l1,)
if tab_lib.count() == 0: ltouple = (l1, ["", ""])
return ltouple
return func_wrapper
但它似乎不起作用
提前致谢
【问题讨论】:
-
这个函数是做什么的,到目前为止你尝试了什么? SO 不是代码编写服务。