Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
decorators
#1
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
Reply
#2
see: http://scottlobdell.me/2015/04/decorator...ts-python/
Reply
#3
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?
Reply
#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
Reply
#5
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.
Reply
#6
(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.
Reply
#7
(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
Reply
#8
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#9
Thank You Wavic!!
Much appreciated.
Reply
#10
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Decorators @ annotation drcl 3 2,310 Feb-24-2024, 06:12 AM
Last Post: Gribouillis
  Does @ at sign used for tother than decorators? ggpython000 1 1,525 Oct-12-2023, 09:08 AM
Last Post: buran
  Variable Scopes Decorators oclmedyb 6 4,613 Jan-15-2021, 02:13 PM
Last Post: oclmedyb
  Decorators and return statements JonEdward 2 3,125 Jul-24-2020, 05:02 PM
Last Post: JonEdward
  How to print cache from Decorators with Memoization OlgaM 2 3,260 Jan-29-2020, 05:06 PM
Last Post: OlgaM
  Python decorators. dodiko1d 0 12,631 Oct-13-2019, 10:23 AM
Last Post: dodiko1d
  Decorators yksingh1097 2 3,744 Aug-14-2018, 01:44 PM
Last Post: yksingh1097
  learning decorators Prince_Bhatia 6 5,225 Aug-13-2018, 02:28 PM
Last Post: Prince_Bhatia
  decorators within decorators mp3909 6 6,514 Apr-02-2018, 09:47 AM
Last Post: hf8bm

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.