On 01/04/2016 09:16 PM, Robert wrote:
Hi,
I find an example code on wrap at this link:
http://stackoverflow.com/questions/308999/what-does-functools-wraps-do
Here is the code:
def logged(func):
def with_logging(*args, **kwargs):
print func.__name__ + " was called"
return func(*args, **kwargs)
return with_logging
///
I understand now, but I feel the args usage is weird. I don't see any way
to use *args and **kwargs in above code. What is your opinion on it?
The reason the inner method has *args, and **kwargs as parameters is so
that it doesn't have to exactly match the signature of func. If func
was a method that took 3 parameters, then if you didn't use *args, you
would have to define the with_logging method to also take 3 parameters.
If func takes one or more keyword parameters, then you would have to
add that to the definition of the with_logging method if you don't use
**kwargs.
IOW, using *args and **kwargs in the wrapper method definition removes
the requirement that its parameter list exactly match func's parameter list.
--
https://mail.python.org/mailman/listinfo/python-list