python - Using decorator from base class both in base class and derived class -
i have python objects methods in check @ beggining, depending of check, method's code run, or execption raised. instead of replicating "check" code @ beginning of every method though of doing decorator, want decorator embedded inside class itself, since closely related it. basically:
instead of this
class a(object): def a_method(self): if self.check_var true: (some_code) else: raise exception
i have this
class a(object): def decorator(function): def function_wrapper(self, *args, **kwargs): if self.check_var true: return function(self, *args, **kwargs) else: raise exception return function_wrapper @decorator def a_method(self): (some_code)
my first question is, going right? or there better way. have many methods of class need have check, why don't want replicate code unnecessarily.
my second question is, if go way described, run problem when want derive class class , performe same decorator checks. again don't want replicate code, want reuse decorator in base class performe checks in derived class. read turning decorator @classmethod
when able use decorator in derived class not in base class anymore!
so this:
class a(object): @classmethod #maybe def decorator(function): def function_wrapper(self, *args, **kwargs): if self.check_var true: return function(self, *args, **kwargs) else: raise exception return function_wrapper @decorator def a_method(self): (some_code) class b(a): @decorator def b_method(self): (some_code)
does know of clean way this?
since prefer put decorator inside class (rather outside both of them suggested in comment), below shows way it. makes decorator staticmethod
instead of classmethod
, , requires using in unusual manner, within class.
for more information regarding necessity of using decorator this, see question calling class staticmethod within the class body?
class a(object): @staticmethod def decorator(function): def function_wrapper(*args, **kwargs): print('in function_wrapper') return function(*args, **kwargs) return function_wrapper @decorator.__func__ #### note unusual decorator usage inside defining class def a_method(self): print('in a_method') class b(a): @a.decorator #### normal decorator usage outside defining class def b_method(self): print('in b_method')
one way avoid having use __func__
, still keep definition in first class postpone turning staticmethod
until end of class definition:
class a(object): def decorator(function): def function_wrapper(*args, **kwargs): print('in function_wrapper') return function(*args, **kwargs) return function_wrapper @decorator def a_method(self): print('in a_method') decorator = staticmethod(decorator) #### convert use outside class class b(a): @a.decorator def b_method(self): print('in b_method')
yet way avoid __func__
this:
class a(object): class check: @staticmethod def decorator(function): def function_wrapper(*args, **kwargs): print('in function_wrapper') return function(*args, **kwargs) return function_wrapper @check.decorator def a_method(self): print('in a_method') class b(a): check = a.check @check.decorator def b_method(self): print('in b_method')
which has additional advantage of making usage of decorator uniform.
Comments
Post a Comment