I have a set of functions in python that gets the same 2 parameters + other parameters.
def myMethodA (param1, param2, specificParm)
do code
def myMethodB (param1, param2, specificParm1 specificParam2)
do code
I waned to create a decorator that replace the need to call with the first 2 parameters by pushing the parameters before calling the function:
@withParams()
def myMethodA (specificParam)
do code
But if I omit the parameters then the decorator can't call it either and if I leave them then the caller need to specify them as well.
anyway to solve this? Can I do something with args* but still have named parameters for specificParam? Also, How can I reference param1 and param2 inside myMethodA
It sounds like you may want functools.partial. It returns a new function with some parameters already specified:
import functools
def withParams(func):
return functools.partial(func,1,2)
@withParams
def myMethodA (param1, param2, specificParam):
print(param1,param2,specificParam)
@withParams
def myMethodB (param1, param2, specificParam1, specificParam2):
print(param1,param2,specificParam1, specificParam2)
myMethodA(10)
myMethodB(12,13)
1 2 10 1 2 12 13
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With