【发布时间】:2017-07-16 19:55:03
【问题描述】:
我在 python 中创建装饰器时遇到了一个有趣的场景。以下是我的代码:-
class RelationShipSearchMgr(object):
@staticmethod
def user_arg_required(obj_func):
def _inner_func(**kwargs):
if "obj_user" not in kwargs:
raise Exception("required argument obj_user missing")
return obj_func(*tupargs, **kwargs)
return _inner_func
@staticmethod
@user_arg_required
def find_father(**search_params):
return RelationShipSearchMgr.search(Relation.RELATION_FATHER, **search_params)
如上面的代码所示,我创建了一个装饰器(这是类中的静态方法),它检查“obj_user”是否作为参数传递给装饰函数。我已经装饰了函数find_father,但我收到以下错误消息:-'staticmethod' object is not callable。
如何使用如上所示的静态实用方法,作为python中的装饰器?
提前致谢。
【问题讨论】:
-
这个答案有帮助吗? stackoverflow.com/a/6412373/4014959
标签: python decorator static-methods