How do I pass an argument z to my decorator function? Here is my code:
def partial(func,z): def wrapper(first, second): first = first*z return func(first, second) return wrapper @partial() #I need to pass z here, how do I do this here? def add(first, second): return first+second
I looked on here, but it doesn't really help as my situation is different - my decorator takes 2 arguments, one of course is the function I want to decorate and the other is just a variable z.
The examples in the post you shown suggest adding another layer to my decorator function which doesn't achieve what I want.
def partial(func,z): def wrapper(first, second): first = first*z return func(first, second) return wrapper return partial @real_decorator(4) def add(first, second): return first+second
I guess what I am trying to ask is how do I write this
add = partial(add,4)
as:
@partial def add(first, second) return first+second
where do I put the 4?
Hopefully this helps
def perma_set_first_param_decorator(x): """ Decorates a two-argument function, replacing the first argument with the provided x value here. """ def wrapper(f): return lambda y: f(x, y) return wrapper @perma_set_first_param_decorator(2) def add(first, second): return first + second print(add(5)) # prints 7
Sorry, I am not sure if people understand my problem.
Let me explain it again.
I have a function called add which takes two arguments and return the addition of them arguments.
Here is my add function:
def add(first, second): return first+second
I now want to decorate this function so that the first argument is always multiplied by 4 and then added to the second argument.
So if add(2,5) then it will return 2*(4)+5 = 13, if add(3,6) then it will return 3*(4)+6 = 18.
(Mar-19-2018, 05:31 PM)mp3909 Wrote: [ -> ]I now want to decorate this function so that the first argument is always multiplied by 4 and then added to the second argument.
From the way you describe it, your decorator will not care what the function it's decorating does. Am I correct in saying what you actually want is a decorator which multiplies the first argument by a value provided to the decorator, before calling the decorated function?
I believe that in either case, my example should be fairly adaptable. Feel free to give it a try, and we can work from there.
(Mar-19-2018, 05:48 PM)micseydel Wrote: [ -> ]From the way you describe it, your decorator will not care what the function it's decorating does. Am I correct in saying what you actually want is a decorator which multiplies the first argument by a value provided to the decorator, before calling the decorated function
- YES!
I have got this far in terms of trying to achieve it:
def primer(func, z): def wrapper(*args,**kwargs): # I need to multiply the first argument by z so something like arg=arg*z before I pass this argument into the function in next line return func(*args, **kwargs) return wrapper @primer def add(first, second): return first+second
In [1]: def decorator(func): ...: def wrapper(first, second): ...: first *= 4 ...: return func(first, second) ...: return wrapper ...: In [2]: @decorator ...: def add(first, second): ...: return first + second ...: In [3]: result = add(1, 4) In [4]: print(result) 8
Thank You Wavic!!
Much appreciated.
I am trying to build decorators within decorators.
I thought I take the example I have discussed previously.
I have a function that multiplies x and y.
My decorator function just prints out a statement saying "x*y = "
I have now added a superdecorator function around my decorator function where I now want to multiply (n*x)*(n*y) i.e. if n=2 then I want to multiply the double of x and the double of y.
Here is my code below but I am getting an error message saying :
print('{} * {} = '.format(*args*n,**kwargs*n), end='') TypeError: unsupported operand type(s) for *: 'dict' and 'int'def superdecorator(n): def decorator(func): def wrapped(*args, **kwargs): print('{} * {} = '.format(*args*n,**kwargs*n), end='') return func(*args*n, **kwargs*n) return wrapped return decorator @superdecorator(2) def mult(x,y): return x*y