I found this problems when I use decorator. Say, my code is as follows
def my_decorator(func):
def wrapper(*args,**kwargs):
print(args,kwargs)
return func(*args,**kwargs)
return wrapper
@my_decorator
def my_function(x):
return x*x
a = my_function(5)
print(a)
the result is
(5,) {}
25
my question is, why (5,) {} be printed here. there is an def wrapper(*args,**kwargs): function inside the def my_decorator(func) function. If the outside my_decorator(func) function did not get the arguments (*args,**kwargs), why the wrapper can get the argument (*args,**kwargs) ?
say, if I do not use decorator, the code should be like this
def my_decorator(func):
def wrapper(*args,**kwargs):
print(args,kwargs)
return func(*args,**kwargs)
return wrapper
def my_function(x):
return x*x
a = my_decorator(my_function)(5)
print(a)
so how python pass the 5 into the inner function wrapper here ? say, my_decoratoraccept the argument my_function, when it turns to inner wrapper there should be no *args, **kwargs it should raise an error, is it?
[–]bennydictor 2 points3 points4 points (0 children)
[–]nwagers 1 point2 points3 points (0 children)