Question: *Python Programming help* write a decorator with arguments so that if you apply: afunction that takes numeric positional or keyword arguments, allthe arguments are raised

*Python Programming help*

write a decorator with arguments so that if you apply: afunction that takes numeric positional or keyword arguments, allthe arguments are raised to the specified power before beingpassed to the decorated function.

We need take a positional or keyword argument and use thedecorator to return the nth power of the argument, but I'mstruggling on it

9 def raiseargs(n): 10 11 12 13 14- 15 16 17- 18

9 def raiseargs(n): 10 11 12 13 14- 15 16 17- 18 19 20 21 wwwDecorator that raises to the n-th power all arguments (positional, or keyword arguments) of a given function.""" # 7 lines #YOUR CODE STARTS HERE def inner(func): def inner2(*args, **kwargs): for_args = map(lambda x: pow(x, n), args) for key, value in kwargs.items(): kwargs.update({key: (value**n)}) return inner2 return inner 22 ## Test cases 23 @raiseargs(2) 24-def f(x, y): 25 return x + y 26 assert f(2, 3) === 13 # 4+ 9 27 @raiseargs(3) 28-def g(x, y=1): 29 return x + y + 1 30 assert g(2, y=2) = 31 == 17 #8 +8 +1 32

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!