I have a Python class with many methods with signature:
def select_xxx(self, arg1 , arg2 , .. argn, intersect = False)
I.e. the methods have a varying (1-3) number of positional arguments, and an optional argument intersect with default value False. I would like to decorate all of these methods with a decorater which will insepect the value of the intersect parameter and take different actions accordingly. My current approach is something like this:
def select_decorator(select_method):
def select_wrapper( self , *args, intersect = False , **kwargs)
if intersect:
# Special init code for intersect == True
select_method( self , *args , **kwargs)
else:
# Normal call path for intersect == False
select_method( self , *args , **kwargs)
return select_wrapper
@select_decorator
select_xxx( self , arg1 , arg2 , intersect = False)
But getting the optional argument intersect into the *args and **kwargs mix inside the decorator is currently no joy. I could sacrifice the **kwargs functionality if that makes the problem easier to solve. Any suggestions?
Joakim
I am assuming that intersect will always be passed as keyword argument. In which case you could simply do this inside you decorator